嗨我有一个xml我需要使用xsl
将该xml转换为另一个xml我的XML看起来像
<Information>
<ShipmentType>
<shipmentType>A</shipmentType>
</ShipmentType>
<ShipmentRouting>
<airportCityCodeOrigin>AAA</airportCityCodeOrigin>
<airportCityCodeDestination>BBB</airportCityCodeDestination>
</ShipmentRouting>
<EarliestDepartureDateTime>
<dayOfMonth>00</dayOfMonth>
<month>OCT</month>
</EarliestDepartureDateTime>
</Information>
应将其转换为以下格式:
<ECIDRA-INP>
<DRA-INP>
<OPTION>
<ORIGIN>
<STATION>AAA</STATION>
</ORIGIN>
<DEST>
<STATION>BBB</STATION>
</DEST>
</OPTION>
</DRA-INP>
</ECIDRA-INP>
我只需要<airportCityCodeOrigin>
和<airportCityCodeDestination>
的值,但我会获得所有标记值。
以下是我写的XSL:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/Information">
<ECIDRA-INP>
<DRA-INP>
<OPTION>
<ORIGIN>
<STATION>
<xsl:value-of select="./ShipmentRouting/airportCodeofDeparture"></xsl:value-of>
</STATION>
</ORIGIN>
<DEST>
<STATION>
<xsl:value-of select="./ShipmentRouting/airportCodeofArrival"></xsl:value-of>
</STATION>
</DEST>
</OPTION>
</DRA-INP>
</ECIDRA-INP>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:0)
AAA BBB 00 OCT是标签的数据还是名称?这很重要,因为修剪数据的方式与“修剪”标签不同。
要创建一个仅包含2个标记名称的新XML,您只需按照自己的意愿编写输出XML,然后引入转换位来提取数据。如果您的目标是特定数据,并希望使用XML(如XSLT),那么您需要编写XPath来选择适当的节点。您的另一种选择是使用具有完整正则表达式功能的语言。
答案 1 :(得分:0)
您的代码存在的问题是它使用了不存在的元素名称,例如airportCodeofDeparture
和airportCodeofArrival
。
请改为:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/Information">
<ECIDRA-INP>
<DRA-INP>
<OPTION>
<ORIGIN>
<STATION>
<xsl:value-of select=
"ShipmentRouting/airportCityCodeOrigin"/>
</STATION>
</ORIGIN>
<DEST>
<STATION>
<xsl:value-of select=
"ShipmentRouting/airportCityCodeDestination"/>
</STATION>
</DEST>
</OPTION>
</DRA-INP>
</ECIDRA-INP>
</xsl:template>
</xsl:stylesheet>
将此转换应用于提供的XML文档:
<Information>
<ShipmentType>
<shipmentType>A</shipmentType>
</ShipmentType>
<ShipmentRouting>
<airportCityCodeOrigin>AAA</airportCityCodeOrigin>
<airportCityCodeDestination>BBB</airportCityCodeDestination>
</ShipmentRouting>
<EarliestDepartureDateTime>
<dayOfMonth>00</dayOfMonth>
<month>OCT</month>
</EarliestDepartureDateTime>
</Information>
产生了想要的正确结果:
<ECIDRA-INP>
<DRA-INP>
<OPTION>
<ORIGIN>
<STATION>AAA</STATION>
</ORIGIN>
<DEST>
<STATION>BBB</STATION>
</DEST>
</OPTION>
</DRA-INP>
</ECIDRA-INP>