2017-04-18 78 views
1

我有以下XML:爲什麼我的XSLT密鑰查找不起作用?

<errorsSortedIntoRows> 
    <row y="044"> 
    <error x="002" y="044" errorClass="early" deviation="-2"/> 
    <error x="002" y="044" errorClass="early" deviation="-5"/> 
    <error x="002" y="044" errorClass="early" deviation="-3"/> 
    </row> 
    <row y="045"> 
    <error x="023" y="045" errorClass="late" deviation="20"/> 
    <error x="023" y="045" errorClass="late" deviation="10"/> 
    <error x="013" y="045" errorClass="wrong" deviation="33"/> 
    <error x="013" y="045" errorClass="wrong" deviation="40"/> 
    </row> 
</errorsSortedIntoRows> 

而且我想消除與問候所有重複x和y:

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

<xsl:key name="xyLookup" match="error" use="concat(@x, '|', @y)"/> 

<xsl:template match="@*|node()"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*|node()"> 
     <xsl:sort select="@y"/> 
     <xsl:sort select="@x"/> 
     <xsl:sort select="@deviation"/> 
    </xsl:apply-templates> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="error[@errorClass = 'early']"> 
    <xsl:value-of select="key('xyLookup', concat(@x, '|', @y))[1]"/> 
</xsl:template> 

<xsl:template match="error[@errorClass = 'late']"> 
    <xsl:value-of select="key('xyLookup', concat(@x, '|', @y))[last()]"/> 
</xsl:template> 

<!-- For cases that are wrong, we just toss the duplicates. --> 
<xsl:template match="error[not(generate-id() = 
           generate-id(key('xyLookup', concat(@x, '|', @y))[1]))]"/> 
</xsl:stylesheet> 

無論出於何種原因,我得到的唯一輸出是行標籤,所有的錯誤標籤已不再存在:

<errorsSortedIntoRows> 
<row y="001"/> 
<row y="002"/> 
<row y="003"/> 
. 
. 
. 
<row y="055"/> 
</errorsSortedIntoRows> 

這甚至用來工作,但我改變了一些標記和屬性的名稱和一切剛剛停止WOR國王。我究竟做錯了什麼?

+0

只是一種預感,但嘗試改變你的'XSL:價值of'對'的xsl:複製of'的。 –

+1

@DanielHaley哇,它確實似乎工作!儘管如此,我還是可以宣誓它的價值。哦,好吧,一切都很好,我想。讓它成爲答案,以便我可以接受它,是嗎? – Fylke

回答

2

試着改變你的xsl:value-of年代到xsl:copy-of的...

<xsl:template match="error[@errorClass = 'early']"> 
    <xsl:copy-of select="key('xyLookup', concat(@x, '|', @y))[1]"/> 
</xsl:template> 

<xsl:template match="error[@errorClass = 'late']"> 
    <xsl:copy-of select="key('xyLookup', concat(@x, '|', @y))[last()]"/> 
</xsl:template>