使用 XSLT 删除第二个元素的重复节点
Remove Duplicate Node of second element using XSLT
我对 xslt 了解不多。我试图从我的输入 xml 中删除一些重复的节点,但这些节点并不完全重复。 PremiseId 值重复,但纬度、经度、XCoordinate 和 YCoordinate 值不同。如果我们在 Input xml 中获得这样的数据,那么我们只需要选择 XML 具有纬度、经度值的节点。目前,我的 xslt 仅拾取第一次出现的重复 PremiseId。
输入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>
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>
预期输出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>-87.36536</Latitude>
<Longitude>30.391645</Longitude>
<Xcoordinate>1556955</Xcoordinate>
<Ycoordinate>-41998772</Ycoordinate>
</ResponseParameter>
</CoordinateCollectionRes>
怎么样:
XSLT 2.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>
我对 xslt 了解不多。我试图从我的输入 xml 中删除一些重复的节点,但这些节点并不完全重复。 PremiseId 值重复,但纬度、经度、XCoordinate 和 YCoordinate 值不同。如果我们在 Input xml 中获得这样的数据,那么我们只需要选择 XML 具有纬度、经度值的节点。目前,我的 xslt 仅拾取第一次出现的重复 PremiseId。
输入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>
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>
预期输出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>-87.36536</Latitude>
<Longitude>30.391645</Longitude>
<Xcoordinate>1556955</Xcoordinate>
<Ycoordinate>-41998772</Ycoordinate>
</ResponseParameter>
</CoordinateCollectionRes>
怎么样:
XSLT 2.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>