开发者

String comparison in XSLT

When I call this template I get the following results.

155IT Matches 155OO
155OO Matches 155OO
155PP

The XML I am processing does have three rows and those are the values, but why is the test returning true for the first two and false for the last one? How should I be doing the string comparison?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template name="ProofOfConcept">
      <xsl:param name="Lines"/>
      <xsl:param name="MainDeliveryCode"/>
      <xsl:choose>
        <xsl:when test="$Lines">
            <xsl:variable name="CurrentDeliveryCode" select="$Lines/DLVYLOCCD"/>
            <p>
            <xsl:choose>
                <xsl:when test=" $MainDeliveryCode = $CurrentDeliveryCode">
<xsl:value-of select="$CurrentDeliveryCode"/> Matches <xsl:value-of select="$MainDeliveryCode"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$Lines"/> Fails <xsl:value-of select="$MainDeliveryCode"/>
                </xsl:otherwise>
            </xsl:choose>
            </p>
            <xsl:call-template name="ProofOfConcept">
                        <xsl:with-param name="Lines" select="$Lines[position() > 1]"/>
                        <xsl:with-param name="MainDeliveryCode" select="$MainDeliveryCode"/>
                     </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
        </xsl:otherwise>
      </xsl:choose>
  </xsl:template>

  <xsl:template match="/">
    <html>
        <head>
            <title></title>
        </head>
        <body>
            <xsl:call-template name="ProofOfConcept">
                <xsl:with-param name="Lines" select="data/Lines/LINE"/>
          开发者_运维百科      <xsl:with-param name="MainDeliveryCode" select="data/header/DLVYLOCCD"/>
            </xsl:call-template>
        </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Sample data

 <?xml version="1.0"
 encoding="ISO-8859-1"
 standalone="yes"?> <data>   
 <header><DLVYLOCCD>155OO</DLVYLOCCD>
 </header>    <Lines>
       <LINE><DLVYLOCCD>155IT</DLVYLOCCD></LINE>
       <LINE><DLVYLOCCD>155OO</DLVYLOCCD></LINE>
       <LINE><DLVYLOCCD>155PP</DLVYLOCCD></LINE>
</Lines> </data>

Thanks for any advice.


Here is a less painful version of your XSLT:

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

  <xsl:template match="data">
    <html>
      <head>
        <title></title>
      </head>
      <body>
        <!-- this selects the matching LINE node(s), or nothing at all -->
        <xsl:apply-templates select="
          Lines/LINE[DLVYLOCCD = /data/header/DLVYLOCCD]
        " />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="LINE">
    <p>
      <!-- for the sake of the example, just output a copy -->
      <xsl:copy-of select="." />
    </p>
  </xsl:template>

</xsl:stylesheet>

gives (formatted result):

<?xml version="1.0" encoding="utf-8"?>
<html>
  <head>
    <title></title>
  </head>
  <body>
    <p>
      <LINE><DLVYLOCCD>155OO</DLVYLOCCD></LINE>
    </p>
  </body>
</html>


There are a few things wrong with your implementation. Most important, the expression:

<xsl:variable name="CurrentDeliveryCode" select="$Lines/DLVYLOCCD"/>

returns a node-set consisting of all the DLVYLOCCD elements, not just the current one as you seem to assume. Also, you shouldn't be using recursion to iterate. Use <xsl:for-each> instead, in which case you will process the items one at a time.


I figured it out.

I need to change my test to

<xsl:when test="contains($MainDeliveryCode, $CurrentDeliveryCode" >

That solved the problem.

http://www.zvon.org/xxl/XSLTreference/Output/function_contains.html is the documentation for the function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜