使用XSLT删除第二个元素的重复节点
我对xslt了解不多。我试图从输入xml中删除一些重复的节点,但这些节点并不完全重复。PremiseId值重复,但纬度、经度、XCoordinate和YCoordinate值不同。如果我们在输入的xml中获得这样的数据,那么我们只需要选取具有纬度、经度值的xml节点。目前,我的xslt只接收第一次出现的重复PremiseId 输入XML:-使用XSLT删除第二个元素的重复节点,xslt,xslt-grouping,Xslt,Xslt Grouping,我对xslt了解不多。我试图从输入xml中删除一些重复的节点,但这些节点并不完全重复。PremiseId值重复,但纬度、经度、XCoordinate和YCoordinate值不同。如果我们在输入的xml中获得这样的数据,那么我们只需要选取具有纬度、经度值的xml节点。目前,我的xslt只接收第一次出现的重复PremiseId 输入XML:- <CoordinateCollectionRes xmlns="http://www.nexteraenergy.org/RetrieveCo
<CoordinateCollectionRes
xmlns="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema"
xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<ResponseParameter>
<PremiseId>42210111</PremiseId>
<Latitude>-80.81082</Latitude>
<Longitude>28.58942</Longitude>
<Xcoordinate>3913026</Xcoordinate>
<Ycoordinate>-42817728</Ycoordinate>
</ResponseParameter>
<ResponseParameter>
<PremiseId>59087449</PremiseId>
<Latitude/>
<Longitude/>
<Xcoordinate>0.0</Xcoordinate>
<Ycoordinate>0.0</Ycoordinate>
</ResponseParameter>
<ResponseParameter>
<PremiseId>60476616</PremiseId>
<Latitude/>
<Longitude/>
<Xcoordinate>0.0</Xcoordinate>
<Ycoordinate>0.0</Ycoordinate>
</ResponseParameter>
<ResponseParameter>
<PremiseId>60476616</PremiseId>
<Latitude>-87.36536</Latitude>
<Longitude>30.391645</Longitude>
<Xcoordinate>1556955</Xcoordinate>
<Ycoordinate>-41998772</Ycoordinate>
</ResponseParameter>
</CoordinateCollectionRes>
<CoordinateCollectionRes xmlns:ns1="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema" xmlns="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema">
<ResponseParameter>
<PremiseId>42210111</PremiseId>
<Latitude>-80.81082</Latitude>
<Longitude>28.58942</Longitude>
<Xcoordinate>3913026</Xcoordinate>
<Ycoordinate>-42817728</Ycoordinate>
</ResponseParameter>
<ResponseParameter>
<PremiseId>59087449</PremiseId>
<Latitude/>
<Longitude/>
<Xcoordinate>0.0</Xcoordinate>
<Ycoordinate>0.0</Ycoordinate>
</ResponseParameter>
<ResponseParameter>
<PremiseId>60476616</PremiseId>
<Latitude>-87.36536</Latitude>
<Longitude>30.391645</Longitude>
<Xcoordinate>1556955</Xcoordinate>
<Ycoordinate>-41998772</Ycoordinate>
</ResponseParameter>
</CoordinateCollectionRes>
42210111
-80.81082
28.58942
3913026
-42817728
59087449
0
0
60476616
0
0
60476616
-87.36536
30.391645
1556955
-41998772
XSLT:-
<?xml version="1.0" encoding="windows-1252" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema">
<xsl:template match="/">
<CoordinateCollectionRes xmlns="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema">
<xsl:for-each-group select="/ns1:CoordinateCollectionRes/ns1:ResponseParameter" group-by="./ns1:PremiseId">
<xsl:copy-of select=".[./ns1:PremiseId=current-grouping-key()]"/>
</xsl:for-each-group>
</CoordinateCollectionRes>
</xsl:template>
</xsl:stylesheet>
输出XML:-
<CoordinateCollectionRes xmlns:ns1="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema" xmlns="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema">
<ResponseParameter>
<PremiseId>42210111</PremiseId>
<Latitude>-80.81082</Latitude>
<Longitude>28.58942</Longitude>
<Xcoordinate>3913026</Xcoordinate>
<Ycoordinate>-42817728</Ycoordinate>
</ResponseParameter>
<ResponseParameter>
<PremiseId>59087449</PremiseId>
<Latitude/>
<Longitude/>
<Xcoordinate>0.0</Xcoordinate>
<Ycoordinate>0.0</Ycoordinate>
</ResponseParameter>
<ResponseParameter>
<PremiseId>60476616</PremiseId>
<Latitude/>
<Longitude/>
<Xcoordinate>0.0</Xcoordinate>
<Ycoordinate>0.0</Ycoordinate>
</ResponseParameter>
</CoordinateCollectionRes>
42210111
-80.81082
28.58942
3913026
-42817728
59087449
0
0
60476616
0
0
预期输出XML:-
<CoordinateCollectionRes
xmlns="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema"
xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<ResponseParameter>
<PremiseId>42210111</PremiseId>
<Latitude>-80.81082</Latitude>
<Longitude>28.58942</Longitude>
<Xcoordinate>3913026</Xcoordinate>
<Ycoordinate>-42817728</Ycoordinate>
</ResponseParameter>
<ResponseParameter>
<PremiseId>59087449</PremiseId>
<Latitude/>
<Longitude/>
<Xcoordinate>0.0</Xcoordinate>
<Ycoordinate>0.0</Ycoordinate>
</ResponseParameter>
<ResponseParameter>
<PremiseId>60476616</PremiseId>
<Latitude/>
<Longitude/>
<Xcoordinate>0.0</Xcoordinate>
<Ycoordinate>0.0</Ycoordinate>
</ResponseParameter>
<ResponseParameter>
<PremiseId>60476616</PremiseId>
<Latitude>-87.36536</Latitude>
<Longitude>30.391645</Longitude>
<Xcoordinate>1556955</Xcoordinate>
<Ycoordinate>-41998772</Ycoordinate>
</ResponseParameter>
</CoordinateCollectionRes>
<CoordinateCollectionRes xmlns:ns1="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema" xmlns="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema">
<ResponseParameter>
<PremiseId>42210111</PremiseId>
<Latitude>-80.81082</Latitude>
<Longitude>28.58942</Longitude>
<Xcoordinate>3913026</Xcoordinate>
<Ycoordinate>-42817728</Ycoordinate>
</ResponseParameter>
<ResponseParameter>
<PremiseId>59087449</PremiseId>
<Latitude/>
<Longitude/>
<Xcoordinate>0.0</Xcoordinate>
<Ycoordinate>0.0</Ycoordinate>
</ResponseParameter>
<ResponseParameter>
<PremiseId>60476616</PremiseId>
<Latitude>-87.36536</Latitude>
<Longitude>30.391645</Longitude>
<Xcoordinate>1556955</Xcoordinate>
<Ycoordinate>-41998772</Ycoordinate>
</ResponseParameter>
</CoordinateCollectionRes>
42210111
-80.81082
28.58942
3913026
-42817728
59087449
0
0
60476616
-87.36536
30.391645
1556955
-41998772
那么:
XSLT2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema"
xpath-default-namespace="http://www.nexteraenergy.org/RetrieveCoordinateResponseSchema">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/CoordinateCollectionRes">
<CoordinateCollectionRes>
<xsl:for-each-group select="ResponseParameter" group-by="PremiseId">
<xsl:copy-of select="(current-group()[string(Latitude|Latitutde)], current-group())[1]" copy-namespaces="no"/>
</xsl:for-each-group>
</CoordinateCollectionRes>
</xsl:template>
</xsl:stylesheet>
演示:我在尝试使用此工具时遇到错误“解析输入XML时出错:文件过早结束”xslt@ShuvraMaitra我包括了一个演示的链接,在那里你可以看到它与你发布的输入相匹配。您引用的错误似乎引用了XML输入,而不是XSLT样式表。因此,您可能使用了不同的输入。