我有一个xml文档,我必须删除基于的重复<itemGrp>
<rateclass>
值。
如果有多个<itemGrp>
具有相同的<rateclass>
,则排除除1 <itemGrp>
以外的所有内容
包括xml和xslt,结果可以在http://www.freeformatter.com/xsl-transformer.html
看到通过粘贴xml和xslt
XSLT:
<xsl:stylesheet version="2.0"
xmlns:a="http://xml.amadeus.com/FQDRES_12_1_1A"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:vbs="urn:schemas-sqlxml-org:vbs">
<!-- Main Template -->
<xsl:template match="/Responses">
<xsl:element name="Fares">
<xsl:for-each select="./RequestResponse/Fare_DisplayFaresForCityPairReply">
<xsl:for-each select="./a:flightDetails">
<!-- inside each flight details, we want to get all of the item groups-->
<xsl:for-each select="./a:itemGrp">
<!-- if rateclass repeating then skip this record other wise add this record to output file-->
<xsl:element name="Calss">
<xsl:value-of select="./a:fareQualifItem/a:additionalFareDetails/a:rateClass"/>
</xsl:element>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
XML:
<Responses>
<RequestResponse>
<Fare_DisplayFaresForCityPairReply xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<flightDetails xmlns="http://xml.amadeus.com/FQDRES_12_1_1A">
<itemGrp>
<fareQualifItem>
<additionalFareDetails>
<rateClass>XK2DCA</rateClass>
</additionalFareDetails>
<discountDetails>
<fareQualifier>725</fareQualifier>
</discountDetails>
</fareQualifItem>
</itemGrp>
<itemGrp>
<fareQualifItem>
<additionalFareDetails>
<rateClass>XK2DCAB</rateClass>
</additionalFareDetails>
<discountDetails>
<fareQualifier>725</fareQualifier>
</discountDetails>
</fareQualifItem>
</itemGrp>
<itemGrp>
<fareQualifItem>
<additionalFareDetails>
<rateClass>XK2DCA</rateClass>
</additionalFareDetails>
<discountDetails>
<fareQualifier>725</fareQualifier>
</discountDetails>
</fareQualifItem>
</itemGrp>
</flightDetails>
<flightDetails xmlns="http://xml.amadeus.com/FQDRES_12_1_1A">
<itemGrp>
<fareQualifItem>
<additionalFareDetails>
<rateClass>AAXK2DCA</rateClass>
</additionalFareDetails>
<discountDetails>
<fareQualifier>725</fareQualifier>
</discountDetails>
</fareQualifItem>
</itemGrp>
<itemGrp>
<fareQualifItem>
<additionalFareDetails>
<rateClass>BXK2DCAB</rateClass>
</additionalFareDetails>
<discountDetails>
<fareQualifier>725</fareQualifier>
</discountDetails>
</fareQualifItem>
</itemGrp>
<itemGrp>
<fareQualifItem>
<additionalFareDetails>
<rateClass>AAXK2DCA</rateClass>
</additionalFareDetails>
<discountDetails>
<fareQualifier>725</fareQualifier>
</discountDetails>
</fareQualifItem>
</itemGrp>
</flightDetails>
</Fare_DisplayFaresForCityPairReply>
</RequestResponse>
</Responses>
回复是:
<?xml version="1.0" encoding="UTF-8"?>
<Fares>
<Calss>XK2DCA</Calss>
<Calss>XK2DCAB</Calss>
<Calss>XK2DCA</Calss>
<Calss>AAXK2DCA</Calss>
<Calss>BXK2DCAB</Calss>
<Calss>AAXK2DCA</Calss>
</Fares>
我想要的是:
<?xml version="1.0" encoding="UTF-8"?>
<Fares>
<Calss>XK2DCA</Calss>
<Calss>XK2DCAB</Calss>
<Calss>AAXK2DCA</Calss>
<Calss>BXK2DCAB</Calss>
</Fares>
谢谢Michael的回答。可能已经解决了:
XSLT 1.0
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="http://xml.amadeus.com/FQDRES_12_1_1A" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:vbs="urn:schemas-sqlxml-org:vbs" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
<!-- Main Template -->
<xsl:key name="rateClass" match="/Responses/RequestResponse/Fare_DisplayFaresForCityPairReply/a:flightDetails/a:itemGrp/a:fareQualifItem/a:additionalFareDetails/a:rateClass/text()" use="." />
<xsl:template match="/Responses">
<xsl:element name="Fares">
<xsl:for-each select="./RequestResponse/Fare_DisplayFaresForCityPairReply">
<xsl:for-each select="./a:flightDetails">
<!-- inside each flight details, we want to get all of the item groups-->
<xsl:for-each select="./a:itemGrp">
<!-- if rateclass repeating then skip this record other wise add this record to output file-->
<xsl:variable name="ItemGroup_Items" select="." />
<!--<xsl:variable name="rateClass_1" select="./a:fareQualifItem/a:additionalFareDetails/a:rateClass"/>
<xsl:if test="vbs:CheckExists($rateClass_1) = 'FALSE'">-->
<xsl:for-each select="./a:fareQualifItem/a:additionalFareDetails/a:rateClass/text()[generate-id()= generate-id(key('rateClass',.)[1])]">
<xsl:element name="Calss">
<xsl:value-of select="." />
</xsl:element>
<xsl:element name="Value_from_itemGrp">
<xsl:value-of select="($ItemGroup_Items/a:fareQualifItem/a:additionalFareDetails/a:rateClass)"/>
</xsl:element>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:1)
鉴于您最新的XML输入,可以通过以下方式非常简单地实现预期输出:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://xml.amadeus.com/FQDRES_12_1_1A"
exclude-result-prefixes="a">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/Responses">
<Fares>
<xsl:for-each select="distinct-values(RequestResponse/Fare_DisplayFaresForCityPairReply/a:flightDetails/a:itemGrp/a:fareQualifItem/a:additionalFareDetails/a:rateClass)">
<Calss>
<xsl:value-of select="."/>
</Calss>
</xsl:for-each>
</Fares>
</xsl:template>
</xsl:stylesheet>