2012-07-25 55 views
0

我通过一个名为StringSplit的模板将一个字符串key6='||1-LIW-3324|1-LIW-3325|1-LIW-3326|1-LIW-3327|1-LIW-4232'与分隔符'|'分开。 我已经从另一个模板中调用此模板StringSplit。因此每个拆分值都存储在SeparatedLineItems中。 现在我想引用调用模板中的每个拆分节点(SeparatedLineItems),并希望将每个分离/拆分节点与另一个变量进行比较。我该怎么做。如何在运行时从调用模板引用由XSLT中的一个模板创建的节点?

这是代码。

<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> 

+0

XSLT 1.0或2.0? – 2012-07-25 15:18:20

回答

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> 

以上,使$键变量的字符串,而序列比包含字符串的元素。如果你喜欢元素,那么你可以使用下面的选择。虽然这种替代方法的缺点是它不包含空的字符串项目。

<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> 
+0

嗨肖恩,谢谢你的回答。但我想要的结果是1-LIW-3的完全匹配。键1中不存在1-LIW-3。所以我想从上面得到的预期结果是:我的$ key6变量是否包含'1-LIW-3'? NO – 2012-07-26 07:41:08

+0

@hero_mca好的。相应更新。 – 2012-07-26 12:15:44

+0

Thansk肖恩,现在它正在工作,但我做了一些修改。它不与msxsl一起工作,所以我使用了xmlns:exslt =“http://exslt.org/common”,然后它就工作了。 :) – 2012-07-26 16:02:55

相关问题