2013-04-04 67 views
0

我的示例xml如下所示:我需要從xml中獲取不同的狀態。我在vs 2010編輯器中使用了xslt 1.0。從xml中獲取不同的值

<?xml version="1.0" encoding="utf-8" ?> 
<states> 
    <node> 
    <value>2</value> 
    <state>DE</state> 
    </node> 
    <node> 
    <value>1</value> 
    <state>DE</state> 
    </node> 
    <node> 
    <value>1</value> 
    <state>NJ</state> 
    </node> 
    <node> 
    <value>1</value> 
    <state>NY</state> 
    </node> 

    <node> 
    <value>1</value> 
    <state>NY</state> 
    </node> 
</states> 

我的XSLT看起來象下面這樣:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
    xmlns:user="urn:my-scripts"> 

    <xsl:output method="text" indent="yes"/> 

    <xsl:key name="st" match="//states/node/state" use="." /> 

    <xsl:variable name="disst"> 
    <xsl:for-each select="//states/node[contains(value,1)]/state[generate-id()=generate-id(key('st',.)[1])]" > 
     <xsl:choose> 
     <xsl:when test="(position() != 1)"> 
      <xsl:value-of select="concat(', ',.)" disable-output-escaping="yes"/> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="." disable-output-escaping="yes"/> 
     </xsl:otherwise> 
     </xsl:choose> 

    </xsl:for-each> 


    </xsl:variable> 

    <xsl:template match="/" > 
     <xsl:value-of disable-output-escaping="yes" select="$disst"/> 

    </xsl:template> 
</xsl:stylesheet> 

輸出:DE,新澤西州,紐約州

我上面的XML是美好的上述測試XML。

如果我更改爲下面的XML:

<?xml version="1.0" encoding="utf-8" ?> 
<states> 
    <node> 
    <value>2</value> 
    <state>DE</state> 
    </node> 
    <node> 
    <value>1</value> 
    <state>DE</state> 
    </node> 
    <node> 
    <value>1</value> 
    <state>NJ</state> 
    </node> 
    <node> 
    <value>1</value> 
    <state>NY</state> 
    </node> 

    <node> 
    <value>1</value> 
    <state>NY</state> 
    </node> 
</states> 

它在未採摘的狀態DE。任何人都可以提出合適的解決方案。提前感謝。 我需要從xml中找出不同的狀態。這裏

回答

2

的問題是你在你的Muenchian分組的XPath的使用謂語:

[contains(value,1)] 

這往往會使Muenchian分組的,不知道所有可用的不同值的。相反,你應該謂詞添加到關鍵:

<xsl:key name="st" match="//states/node[contains(value, 1)]/state" use="." /> 

或者,您可以應用謂詞分組聲明:

<xsl:apply-templates 
    select="//states/node 
       /state[generate-id() = 
         generate-id(key('st',.)[contains(../value, 1)][1])]" /> 

完全XSLT(有一些改進):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:user="urn:my-scripts"> 
    <xsl:output method="text" indent="yes"/> 

    <xsl:key name="st" match="//states/node/state" use="." /> 
    <xsl:variable name="a" select="1" /> 

    <xsl:variable name="disst"> 
    <xsl:apply-templates 
     select="//states/node 
        /state[generate-id() = 
          generate-id(key('st',.)[contains(../value, $a)][1])]" /> 
    </xsl:variable> 

    <xsl:template match="state"> 
    <xsl:if test="position() > 1"> 
     <xsl:text>,</xsl:text> 
    </xsl:if> 
    <xsl:value-of select ="." disable-output-escaping="yes" /> 
    </xsl:template> 

    <xsl:template match="/" > 
    <xsl:value-of disable-output-escaping="yes" select="$disst"/> 

    </xsl:template> 
</xsl:stylesheet> 

在您的示例XML上運行時的結果:

DE,NJ,NY 
+0

如果我有這樣的情景: 1 Blossom 2013-04-04 19:44:27

+0

上面做了必要的修改。 – JLRishe 2013-04-04 19:50:01

+0

謝謝Rishe。 – Blossom 2013-04-08 13:19:30