2012-07-25 20 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