2013-06-12 25 views
6

Zadeklaruj zmienną globalną w XSLT, ponownie przypisać wartość lokalnie

mogę zadeklarować zmienną „myVariable” o wartości „111” w zakresie globalnym. Ale jak mogę ponownie przypisać wartość lokalnie. Czy istnieje alternatywny sposób, aby to osiągnąć.

Proszę o pomoc. Dziękuję. Ravi

+0

Tak, istnieje alternatywny sposób rozwiązania problemu. Chętnie wyjaśnimy to po wyjaśnieniu problemu (zamiast błędnej próby rozwiązania). –

Odpowiedz

6

Możesz ponownie zdefiniować tę samą zmienną wewnątrz szablonu:

<xsl:variable name="myVariable" select="'111'"/> 

<xsl:template match="/"> 
    <xsl:variable name="myVariable" select="'112'"/> 
    . . . 
</xsl:template> 

Zauważ jednak, że „zmienne” w XSLT są rzeczywiście stała - nie są ponownie przypisując inną wartość do tej samej zmiennej , ponownie definiujesz go wewnątrz szablonu - poza szablonem myVariable nadal będzie mieć wartość 111.

+9

To nie jest "" redefiniowanie tej samej zmiennej "*, definiuje nową zmienną o tej samej nazwie. – Borodin

+0

Dziękuję za odpowiedź .. To nie będzie rozwiązanie, ponieważ nie mogę mieć innej wartości przypisanej zmiennej "myVariable" –

+1

@Ravi: nie ma bezpośredniego "rozwiązania" - w XSLT nie można zmienić wartości zmiennej. Jeśli rozwiniesz pytanie wyjaśniające, co musisz osiągnąć, możemy spróbować zaproponować rozwiązania, które nie wymagają zmiany wartości zmiennej – MiMo

-2

Możesz osiągnąć to, co chcesz zrobić, używając jscript/vbscript. Oto przykład użycia Jscript.

<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns="http://www.w3.org/TR/xhtml1/strict" 
       xmlns:msxsl='urn:schemas-microsoft-com:xslt' 
       xmlns:var='urn:var' 
       xmlns:JS='urn:JS' 
       > 
    <xsl:output method="html"/> 
    <xsl:variable name="n" select="1"/> 
    <xsl:template match="/NewDataSet"> 
    <html> 
     <head> 
     <style> 
      table{border-collapse:collapse} 
      table,td{border:1px solid black;color:black; background-color:white} 
      table,th{border:1px solid balck;background-color:black;color:white } 
      .rt{color:red} 
      .redb{color:yellow; background-color:red;} 
      .greenb{color:white;background-color:green;} 
     </style> 
     <title>EDI validation Result </title> 
     </head> 
     <body> 
     <div font="bold"> 
      EDI validation result for the PO <xsl:value-of select="info/pono"/> 
     </div> 
     <div> 
      received from <xsl:value-of select="info/CustomerName"/> 
     </div> 
     <xsl:variable name='var:hasErrors' select='0'/> 
     <xsl:variable name='var:ngoodlines' select='0' /> 
     <xsl:variable name='var:nbadlines' select='0' /> 
     <table> 
      <th>Position</th> 
      <th>Item Code</th> 
      <th>UoM</th> 
      <th>Ordered Qty.</th> 
      <th>Net-Quoted</th> 
      <th>Net-Catalog</th> 
      <th>Status</th> 
      <xsl:for-each select="Table"> 
      <tr> 
       <xsl:choose> 
       <xsl:when test="Status !=''"> 
        <xsl:value-of disable-output-escaping="yes" select="JS:IncBlines()"/> 
        <td class="redb"><xsl:value-of select="Position"/></td> 
        <td class="redb"><xsl:value-of select="ItemCode"/></td> 
        <td class="redb"><xsl:value-of select="UoM"/></td> 
        <td class="redb"><xsl:value-of select="QtyOrdered"/></td> 
        <td class="redb"><xsl:value-of select="PriceQuoted"/></td> 
        <td class="redb"><xsl:value-of select="Net"/></td> 
        <td class="redb"><xsl:value-of select="Status"/></td> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:value-of select="JS:IncGlines()"/> 

        <td class="greenb"><xsl:value-of select="Position"/></td> 
        <td class="greenb"><xsl:value-of select="ItemCode"/></td> 
        <td class="greenb"><xsl:value-of select="UoM"/></td> 
        <td class="greenb"><xsl:value-of select="QtyOrdered"/></td> 
        <td class="greenb"><xsl:value-of select="PriceQuoted"/></td> 
        <td class="greenb"><xsl:value-of select="Net"/></td> 
        <td class="greenb"><xsl:value-of select="Status"/>OK</td> 

       </xsl:otherwise> 
       </xsl:choose> 
      </tr> 
      </xsl:for-each> 
     </table> 

     <xsl:if test="JS:GetBlines() > 0" > 

      <div class="rt"> 
      <p> 
       The order validation has failed , 
       The order will not be processesed as there are <xsl:value-of select ="JS:GetBlines()"/> lines in error. 
       <xsl:if test="JS:GetGlines() > 0"> 
       Although there are <xsl:value-of select="JS:GetGlines()"/> line item(s) are that comply the requirements, <p> 
       The P.O is rejected as per agreed processing rules. 
        </p> 

       </xsl:if> 

      </p> 

      </div> 

     </xsl:if> 


      <xsl:value-of select="JS:GetBlines()"/> 
      <xsl:value-of select ="JS:GetGlines()"/> 

     </body> 
    </html> 
    </xsl:template> 
    <msxsl:script language='JScript' implements-prefix='JS'> 

    <![CDATA[ 
    var j :int=0; 
    var blines:int =0; 
    var glines:int=0; 
    function Inc(current) 
    {j=j+current; 

    return j+current; 
    } 
    function IncBlines() 
    { 
    blines++; 
    } 
    function IncGlines() 
    { 
    glines++; 
    } 

    function GetBlines() 
    { 
    return blines; 
    } 
    function GetGlines() 
    { 
    return glines; 
    } 
]]> 
    </msxsl:script> 
</xsl:stylesheet> 

<NewDataSet> 
    <Table> 
    <Position>1</Position> 
    <ItemCode>691-301-004</ItemCode> 
    <QtyOrdered>1</QtyOrdered> 
    <PriceQuoted>0.00</PriceQuoted> 
    <Net>0.0000</Net> 
    <UoM>EA</UoM> 
    <Status>Not in Catalog</Status> 
    </Table> 
    <Table> 
    <Position>2</Position> 
    <ItemCode>106284-133</ItemCode> 
    <QtyOrdered>1</QtyOrdered> 
    <PriceQuoted>20.00</PriceQuoted> 
    <Net>0.0000</Net> 
    <UoM>EA</UoM> 
    <Status>Not in Catalog</Status> 
    </Table> 
    <Table> 
    <Position>3</Position> 
    <ItemCode>116304-317</ItemCode> 
    <QtyOrdered>1</QtyOrdered> 
    <PriceQuoted>25.00</PriceQuoted> 
    <Net>0.0000</Net> 
    <UoM>EA</UoM> 
    <Status>Not in Catalog</Status> 
    </Table> 
    <Table> 
    <Position>4</Position> 
    <ItemCode>574116-035</ItemCode> 
    <QtyOrdered>10</QtyOrdered> 
    <PriceQuoted>1598.85</PriceQuoted> 
    <Net>1865.5000</Net> 
    <QtyApplicable>99999999</QtyApplicable> 
    <UoM>EA</UoM> 
    <Status>Quoted Price does not match Catalog price</Status> 
    </Table> 
    <Table> 
    <Position>5</Position> 
    <ItemCode>110223-301</ItemCode> 
    <QtyOrdered>10</QtyOrdered> 
    <PriceQuoted>147.88</PriceQuoted> 
    <Net>147.8800</Net> 
    <QtyApplicable>99999999</QtyApplicable> 
    <UoM>EA</UoM> 
    </Table> 
    <info> 
    <baanid>xxx-xxxxxx</baanid> 
    <pono>xxx0002987</pono> 
    <CustomerName>[xxx-xxxxxx]-xxxxxxxxxxxxxxxxxxxxxxxxx x xxxxx/CustomerName> 
    </info> 
</NewDataSet> 
Powiązane problemy