0

我通过一个key6='||1-LIW-3324|1-LIW-3325|1-LIW-3326|1-LIW-3327|1-LIW-4232'名为. 我从另一个模板中调用了这个模板。所以每个拆分值都存储在. 现在我想在调用模板中引用每个拆分节点(),并希望将每个分离/拆分节点与另一个变量进行比较。我该怎么做。'|'StringSplitStringSplitSeparatedLineItemsSeparatedLineItems

这是代码。

<xsl:template match="/">
    <xsl:variable name="check3">
        <xsl:value-of select="1-LIW-3"/>
    </xsl:variable>

    <myProj>
    <xsl:call-template name="StringSplit">
            <xsl:with-param name="val" select="$key6"/>
    </xsl:call-template>

    <!--here I want to compare "SeperatedLineItems" with $check3 -->                   
    <!--<xsl:variable name="con"> 
        <xsl:value-of select="contains($key6,$check3)"/> 
    </xsl:variable>-->        
    </myProj>

</xsl:template>

<xsl:template name="StringSplit">
    <xsl:param name="val" select="$key6"/>
    <!-- do a check to see if the input string (still) has a "|" in it -->
    <xsl:choose>
        <xsl:when test="contains($val, '|')">
            <!-- pull out the value of the string before the "|" delimiter -->
            <SeperatedLineItems>
                <xsl:value-of select="substring-before($val, '|')"/>
            </SeperatedLineItems> 
            <!-- recursively call this template and pass in value AFTER the "|" delimiter -->
            <xsl:call-template name="StringSplit">
                <xsl:with-param name="val" select="substring-after($val, '|')"/> 
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <!-- if there is no more delimiter values, print out the whole string -->
            <SeperatedLineItems>
                <xsl:value-of select="$val"/>
            </SeperatedLineItems>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

4

1 回答 1

0

这个 XSLT 1.0 样式表演示了如何访问通过调用模板返回的元素......

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  exclude-result-prefixes="xsl msxsl">
<xsl:output method="text"/>

<xsl:variable name="key6" select="'||1-LIW-3324|1-LIW-3325|1-LIW-3326|1-LIW-3327|1-LIW-4232'" />  

<xsl:template match="/">
Does my $key6 variable contain precisely '1-LIW-3'?
 <xsl:variable name="keys"> 
  <xsl:call-template name="StringSplit">
    <xsl:with-param name="val" select="$key6"/>
  </xsl:call-template>
 </xsl:variable>
 <xsl:choose> 
   <xsl:when test="msxsl:node-set($keys)/*[. = '1-LIW-3']">
    YES
  </xsl:when>
  <xsl:otherwise> 
    NO
  </xsl:otherwise> 
 </xsl:choose> 
</xsl:template>

<xsl:template name="StringSplit">
  <xsl:param name="val" />
  <xsl:choose>
   <xsl:when test="contains($val,'|')">
    <SeperatedLineItems>
      <xsl:value-of select="substring-before($val,'|')" />
    </SeperatedLineItems>
    <xsl:variable name="remainder"> 
     <xsl:call-template name="StringSplit">
       <xsl:with-param name="val" select="substring-after($val,'|')" />
     </xsl:call-template>
    </xsl:variable>
    <xsl:copy-of select="msxsl:node-set($remainder)/*" /> 
   </xsl:when>
   <xsl:when test="$val != ''">
    <SeperatedLineItems>
      <xsl:value-of select="$val" />
    </SeperatedLineItems>
  </xsl:when>
  <xsl:otherwise> 
  </xsl:otherwise> 
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>

结果是……

Does my $key6 variable contain '1-LIW-3'?

    NO

这是 XSLT 2.0 等价物...

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  exclude-result-prefixes="xsl xs">
<xsl:output method="text"/>

<xsl:variable name="key6" select="'||1-LIW-3324|1-LIW-3325|1-LIW-3326|1-LIW-3327|1-LIW-4232'" />  

<xsl:template match="/">
Does my $key6 variable contain precisely '1-LIW-3'?
 <xsl:variable name="keys" select="tokenize($key6,'\|')" as="xs:string*" /> 
 <xsl:choose> 
   <xsl:when test="some $key in $keys satisfies ($key = '1-LIW-3')">
    YES
  </xsl:when>
  <xsl:otherwise> 
    NO
  </xsl:otherwise> 
 </xsl:choose> 
</xsl:template>

</xsl:stylesheet>

以上使 $keys 变量成为字符串序列,而不是包含字符串的元素。如果您更喜欢元素,那么您可以使用以下替代方法。尽管此替代方案的缺点是它不包括空字符串项。

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  exclude-result-prefixes="xsl xs">
<xsl:output method="text"/>

<xsl:variable name="key6" select="'||1-LIW-3324|1-LIW-3325|1-LIW-3326|1-LIW-3327|1-LIW-4232'" />  

<xsl:template match="/">
Does my $key6 variable contain precisely '1-LIW-3'?
 <xsl:variable name="keys" as="element()*" > 
  <xsl:analyze-string select="concat($key6,'|')" regex="([^\|]*)\|">
   <xsl:matching-substring>  
    <SeperatedLineItems>
     <xsl:value-of select="regex-group(1)" />
    </SeperatedLineItems>
   </xsl:matching-substring>  
  </xsl:analyze-string> 
 </xsl:variable> 
 <xsl:choose> 
   <xsl:when test="$keys[. = '1-LIW-3']">
    YES
  </xsl:when>
  <xsl:otherwise> 
    NO
  </xsl:otherwise> 
 </xsl:choose> 
</xsl:template>

</xsl:stylesheet>
于 2012-07-25T17:52:24.810 回答