2011-02-01 63 views
1

您好我有一个XML我需要使用XSL将XML转换为另一种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

“我的XML看上去l IKE” ..;你的意思是没有尖括号的XML? ;-)请给我们展示真正的XML(和真正的XSLT)。 – 2011-02-01 14:28:51

回答

0

是一个AAA BBB 00 OCT数据或标记的名称?这很重要,因为修剪数据的方式与“修剪”标记不同。

要创建一个只包含5个标签名称的新XML,您只需按照需要编写输出XML,然后引入转换位来提取数据。如果您针对的是特定数据,并且希望使用XML(如XSLT),那么您需要编写XPath来选择合适的节点。你的其他选择是在你有完整的Regex功能的地方使用一种语言。

0

代码的问题在于它使用不存在的元素名称,例如airportCodeofDepartureairportCodeofArrival

只是正确的:

<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>