2017-01-10 44 views
0

我想試試這個例子:如何從xml,xslt獲得正確的結果?

文件:data.xml中

<?xml version="1.0"?> 
<?xml-stylesheet type="text/xsl" href="Transform.xslt"?> 
<addressbook> 
    <address> 
    <first-name>Doris</first-name> 
    <last-name>Smith</last-name> 
    <city>New York</city> 
    <state>WI</state> 
    </address> 
    <address> 
    <first-name>Mary</first-name> 
    <last-name>Smith</last-name> 
    <city>Vancouver</city> 
    <state>MA</state> 
    </address> 

文件:Transform.xslt

<?xml version="1.0"?> 
<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output method="text"/> 

    <xsl:template match="/"> 
    <xsl:text>Customers grouped by state&#xA;&#xA;</xsl:text> 
    <xsl:for-each-group select="/addressbook/address" group-by="state"> 
     <xsl:sort select="state"/> 
     <xsl:text> State = </xsl:text> 
     <xsl:value-of select="current-grouping-key()"/> 
     <xsl:text>&#xA;</xsl:text> 
     <xsl:for-each select="current-group()"> 
     <xsl:text>&#x9;</xsl:text> 
     <xsl:value-of select="(first-name, last-name)" separator=" "/> 
     <xsl:text>, </xsl:text> 
     <xsl:value-of select="city"/> 
     <xsl:text>&#xA;</xsl:text> 
     </xsl:for-each> 
    </xsl:for-each-group> 
    </xsl:template> 

</xsl:stylesheet> 

預期輸出是:

Customers grouped by state 

    State = MA 
    Mary Smith, Vancouver 
    State = WI 
    Doris Smith, New York 

但我擁有的是:

多麗絲史密斯紐約威斯康星州瑪麗史密斯溫哥華馬爾文

如果你能向我解釋問題,我將不勝感激。

+0

如果你想使用瀏覽器來處理你的XSLT(這是基於'xml-stylesheet'處理指令的樣子),你將無法使用XSLT 2.0 (這意味着你不能使用'xsl:for-each-group';你必須使用[Muenchian分組](http://www.jenitennison.com/xslt/grouping/muenchian.html))。 –

+0

雖然在XSLT 2.0上做得不錯。它確實在使用2.0處理器時能夠帶來預期的結果。 (http://xsltransform.net/bFWR5Eu) –

+0

你基本上看到原始XML中沒有標記的文本,這強烈表明問題在於你調用(或未能調用)轉換的方式,而不是XSLT代碼本身。 –

回答

0

看來您正在使用瀏覽器根據XML文件中的xml-stylesheet處理指令處理您的XSLT。

不幸的是,沒有像Saxon-CESaxon-JS你不會要能夠在瀏覽器中運行XSLT 2.0轉換。

下面是一個使用Muenchian Grouping應該工作的XSLT 1.0選項...

XML輸入

<?xml-stylesheet type="text/xsl" href="Transform.xslt"?> 
<addressbook> 
    <address> 
    <first-name>Doris</first-name> 
    <last-name>Smith</last-name> 
    <city>New York</city> 
    <state>WI</state> 
    </address> 
    <address> 
    <first-name>Mary</first-name> 
    <last-name>Smith</last-name> 
    <city>Vancouver</city> 
    <state>MA</state> 
    </address> 
</addressbook> 

XSLT 1.0(Transform.xslt)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:key name="address" match="address" use="state"/> 

    <xsl:template match="/addressbook"> 
    <xsl:text>Customers grouped by state&#xA;&#xA;</xsl:text> 
    <xsl:for-each select="address[count(.|key('address',state)[1])=1]"> 
     <xsl:sort select="state"/> 
     <xsl:value-of select="concat('&#x9;',state,'&#xA;')"/> 
     <xsl:for-each select="key('address',state)"> 
     <xsl:value-of select="concat('&#x9;', 
      first-name,' ',last-name, 
      ', ',city,'&#xA;')"/> 
     </xsl:for-each> 
    </xsl:for-each> 
    </xsl:template> 

</xsl:stylesheet> 

輸出

Customers grouped by state 

    MA 
    Mary Smith, Vancouver 
    WI 
    Doris Smith, New York