在XSLT中声明全局变量,在本地重新赋值

时间:2013-06-12 13:11:31

标签: variables xslt

我可以在全局范围内声明一个值为“111”的变量“myVariable”。 但是如何在本地重新分配值。或者是否有另一种方法来实现这一目标。

请帮忙。 谢谢。 拉维

2 个答案:

答案 0 :(得分:7)

您可以在模板中重新定义相同的变量:

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

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

请注意,虽然XSLT中的“变量”实际上是常量 - 您没有为同一个变量重新分配不同的值,而是在模板中重新定义它 - 在模板myVariable之外仍然会有值111

答案 1 :(得分:-2)

您可以使用jscript / vbscript完成您想要的操作。这是使用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>