2017-04-18 79 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>