使用Xslt从XML中删除重复条目 1. 1. 13 13 12 100 sumit正在测试 测试一下 3. 3. 4. 3. 4.
输出应该是使用Xslt从XML中删除重复条目 1. 1. 13 13 12 100 sumit正在测试 测试一下 3. 3. 4. 3. 4.,xml,xslt,Xml,Xslt,输出应该是 <LISTINGS> <LISTING LISTING_ID="123456789"> <NAME1>1</NAME1> <NAME1>1</NAME1> <NAME1>13</NAME1> <NAME1>13</NAME1> <NAME1>12</NAME1> <NAME1>100
<LISTINGS>
<LISTING LISTING_ID="123456789">
<NAME1>1</NAME1>
<NAME1>1</NAME1>
<NAME1>13</NAME1>
<NAME1>13</NAME1>
<NAME1>12</NAME1>
<NAME1>100</NAME1>
<NAME1>sumit is testing</NAME1>
<NAME1>TEST IT</NAME1>
</LISTING>
<LISTING LISTING_ID="987654321">
<NAME1>3</NAME1>
<NAME1>3</NAME1>
<NAME1>4</NAME1>
</LISTING>
<LISTING LISTING_ID="5656566565">
<NAME1>3</NAME1>
<NAME1>4</NAME1>
</LISTING>
</LISTINGS>
1.
13
12
100
sumit正在测试
测试一下
3.
4.
3.
4.
此样式表:
<LISTINGS>
<LISTING LISTING_ID="123456789">
<NAME1>1</NAME1>
<NAME1>13</NAME1>
<NAME1>12</NAME1>
<NAME1>100</NAME1>
<NAME1>sumit is testing</NAME1>
<NAME1>TEST IT</NAME1>
</LISTING>
<LISTING LISTING_ID="987654321">
<NAME1>3</NAME1>
<NAME1>4</NAME1>
</LISTING>
<LISTING LISTING_ID="5656566565">
<NAME1>3</NAME1>
<NAME1>4</NAME1>
</LISTING>
</LISTINGS>
输出:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="kElementByListingIdAndValue"
match="LISTING/*"
use="concat(../@LISTING_ID,'+',.)"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="LISTING/*[count(.|key('kElementByListingIdAndValue',
concat(../@LISTING_ID,
'+',
.)
)[1]) != 1]"/>
</xsl:stylesheet>
1.
13
12
100
sumit正在测试
测试一下
3.
4.
3.
4.
使用Muenchian方法进行分组:
<LISTINGS>
<LISTING LISTING_ID="123456789">
<NAME1>1</NAME1>
<NAME1>13</NAME1>
<NAME1>12</NAME1>
<NAME1>100</NAME1>
<NAME1>sumit is testing</NAME1>
<NAME1>TEST IT</NAME1>
</LISTING>
<LISTING LISTING_ID="987654321">
<NAME1>3</NAME1>
<NAME1>4</NAME1>
</LISTING>
<LISTING LISTING_ID="5656566565">
<NAME1>3</NAME1>
<NAME1>4</NAME1>
</LISTING>
</LISTINGS>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kListNameByVal" match="LISTING/NAME1"
use="concat(generate-id(..),'+',.)"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"NAME1
[not(generate-id()
=
generate-id(key('kListNameByVal',
concat(generate-id(..),'+',.)
)
[1]
)
)
]
"/>
</xsl:stylesheet>
<LISTINGS>
<LISTING LISTING_ID="123456789">
<NAME1>1</NAME1>
<NAME1>1</NAME1>
<NAME1>13</NAME1>
<NAME1>13</NAME1>
<NAME1>12</NAME1>
<NAME1>100</NAME1>
<NAME1>sumit is testing</NAME1>
<NAME1>TEST IT</NAME1>
</LISTING>
<LISTING LISTING_ID="987654321">
<NAME1>3</NAME1>
<NAME1>3</NAME1>
<NAME1>4</NAME1>
</LISTING>
<LISTING LISTING_ID="5656566565">
<NAME1>3</NAME1>
<NAME1>4</NAME1>
</LISTING>
</LISTINGS>
应用于提供的XML文档时:
<LISTINGS>
<LISTING LISTING_ID="123456789">
<NAME1>1</NAME1>
<NAME1>13</NAME1>
<NAME1>12</NAME1>
<NAME1>100</NAME1>
<NAME1>sumit is testing</NAME1>
<NAME1>TEST IT</NAME1>
</LISTING>
<LISTING LISTING_ID="987654321">
<NAME1>3</NAME1>
<NAME1>4</NAME1>
</LISTING>
<LISTING LISTING_ID="5656566565">
<NAME1>3</NAME1>
<NAME1>4</NAME1>
</LISTING>
</LISTINGS>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kListNameByVal" match="LISTING/NAME1"
use="concat(generate-id(..),'+',.)"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"NAME1
[not(generate-id()
=
generate-id(key('kListNameByVal',
concat(generate-id(..),'+',.)
)
[1]
)
)
]
"/>
</xsl:stylesheet>
<LISTINGS>
<LISTING LISTING_ID="123456789">
<NAME1>1</NAME1>
<NAME1>1</NAME1>
<NAME1>13</NAME1>
<NAME1>13</NAME1>
<NAME1>12</NAME1>
<NAME1>100</NAME1>
<NAME1>sumit is testing</NAME1>
<NAME1>TEST IT</NAME1>
</LISTING>
<LISTING LISTING_ID="987654321">
<NAME1>3</NAME1>
<NAME1>3</NAME1>
<NAME1>4</NAME1>
</LISTING>
<LISTING LISTING_ID="5656566565">
<NAME1>3</NAME1>
<NAME1>4</NAME1>
</LISTING>
</LISTINGS>
1.
1.
13
13
12
100
sumit正在测试
测试一下
3.
3.
4.
3.
4.
生成所需的正确结果:
<LISTINGS>
<LISTING LISTING_ID="123456789">
<NAME1>1</NAME1>
<NAME1>13</NAME1>
<NAME1>12</NAME1>
<NAME1>100</NAME1>
<NAME1>sumit is testing</NAME1>
<NAME1>TEST IT</NAME1>
</LISTING>
<LISTING LISTING_ID="987654321">
<NAME1>3</NAME1>
<NAME1>4</NAME1>
</LISTING>
<LISTING LISTING_ID="5656566565">
<NAME1>3</NAME1>
<NAME1>4</NAME1>
</LISTING>
</LISTINGS>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kListNameByVal" match="LISTING/NAME1"
use="concat(generate-id(..),'+',.)"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"NAME1
[not(generate-id()
=
generate-id(key('kListNameByVal',
concat(generate-id(..),'+',.)
)
[1]
)
)
]
"/>
</xsl:stylesheet>
<LISTINGS>
<LISTING LISTING_ID="123456789">
<NAME1>1</NAME1>
<NAME1>1</NAME1>
<NAME1>13</NAME1>
<NAME1>13</NAME1>
<NAME1>12</NAME1>
<NAME1>100</NAME1>
<NAME1>sumit is testing</NAME1>
<NAME1>TEST IT</NAME1>
</LISTING>
<LISTING LISTING_ID="987654321">
<NAME1>3</NAME1>
<NAME1>3</NAME1>
<NAME1>4</NAME1>
</LISTING>
<LISTING LISTING_ID="5656566565">
<NAME1>3</NAME1>
<NAME1>4</NAME1>
</LISTING>
</LISTINGS>
1.
13
12
100
sumit正在测试
测试一下
3.
4.
3.
4.
问得好,+1。请参阅我的答案,以了解Muenchian分组方法的演示——简短而高效的完整解决方案。您能在这里指出[1]的含义吗?我一直认为,应用于键的generate-id()将始终生成与键模式匹配的第一个元素的id。另外,没有这个谓词,您的代码运行得很好。@Flack:Muenchian方法选择同类中的第一个,这就是为什么您必须将节点(在本例中为generate-id()
)与具有相同键的第一个(按文档顺序)节点进行比较的原因。@Alejandro,谢谢,我明白了。尽管如此,您能否提供一个示例链接,其中必要性更为明显。我只是想巩固一点知识。@Flack:是的,但我总是写[1]
来把这个事实说清楚。许多人忘记或不知道这样一个事实,即当generate-id()
的参数是一个节点集时,它只从该节点集中按文档顺序取第一个节点。与
完全相同。