XSLT没有正确转换

时间:2013-02-13 20:50:46

标签: xslt xml-parsing

我的XML文件:

<result>
    <xml_acc>
        <CheckInFrom>01:15</CheckInFrom>
        <CheckInTo>00:30</CheckInTo>
        <CheckOutFrom>00:45</CheckOutFrom>
        <CheckOutTo>01:15</CheckOutTo>
        <hotel_id>1</hotel_id>
        <name>Tahir Hotel</name>
        <hoteltype>Tahir Hotel</hoteltype>
        <CityID>1</CityID>
        <city>London</city>
        <CountryID>26</CountryID>
        <CurrencyId>26</CurrencyId>
        <languagecode>1</languagecode>
        <Latitude>13131313</Latitude>
        <Longitude>11131131</Longitude>
        <max_persons_in_reservation>1000</max_persons_in_reservation>
        <ranking>1</ranking>
        <url>123Testing</url>
        <zip>W2 6DX</zip>
    </xml_acc>
    <xml_acc>
        <CheckInFrom>01:15</CheckInFrom>
        <CheckInTo>00:30</CheckInTo>
        <CheckOutFrom>00:45</CheckOutFrom>
        <CheckOutTo>01:15</CheckOutTo>
        <hotel_id>1</hotel_id>
        <name>Tahir Hotel</name>
        <hoteltype>Tahir Hotel</hoteltype>
        <CityID>1</CityID>
        <city>London</city>
        <CountryID>26</CountryID>
        <CurrencyId>26</CurrencyId>
        <languagecode>1</languagecode>
        <Latitude>13131313</Latitude>
        <Longitude>11131131</Longitude>
        <max_persons_in_reservation>1000</max_persons_in_reservation>
        <ranking>1</ranking>
        <url>123Testing</url>
        <zip>W2 6DX</zip>
    </xml_acc>
</result>

我正在申请以下XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" indent="yes"  method="html" />
  <xsl:strip-space elements="*"/>

  <xsl:template match="xml_acc">
      <result>
    <!--<xsl:apply-templates select="node()[not(self::CheckInTo) and not(self::CheckInFrom)][not(self::CheckOutTo) and not(self::CheckOutFrom)]"/>-->
    <Address>
      <xsl:apply-templates select="Address"/>
    </Address>
    <checkin>
      <from>
        <xsl:apply-templates select="CheckInTo"/>
      </from>
      <to>
        <xsl:apply-templates select="CheckInFrom"/>
      </to>
    </checkin>
    <city>
      <xsl:apply-templates select="city"/>
    </city>
    <city_id>
     <xsl:apply-templates select="CityID"/>
    </city_id>
    <Location>
      <Latitude>
          <xsl:apply-templates select="Latitude"/>
        </Latitude>
        <Longitude>
          <xsl:apply-templates select="Longitude"/>
        </Longitude>
    </Location>
    <city_id>
      <xsl:apply-templates select="CityID"/>
    </city_id>
    <max_persons_in_reservation>
      <xsl:apply-templates select="max_persons_in_reservation"/>
    </max_persons_in_reservation>
    <name>
      <xsl:apply-templates select="name"/>
    </name>
    <ranking>
      <xsl:apply-templates select="ranking"/>
    </ranking>
    <url>
      <xsl:apply-templates select="url"/>
    </url>
    <zip>
      <xsl:apply-templates select="zip"/>
    </zip>
      </result>
  </xsl:template>
</xsl:stylesheet>

我得到了结果:

<result>
    <Address>
    </Address>
    <checkin>
        <from>00:30</from>
        <to>01:15</to>
    </checkin>
    <city>London</city>
    <city_id>1</city_id>
    <Location>
        <Latitude>13131313</Latitude>
        <Longitude>11131131</Longitude>
    </Location>
    <city_id>1</city_id>
    <max_persons_in_reservation>1000</max_persons_in_reservation>
    <name>Tahir Hotel</name>
    <ranking>1</ranking>
    <url>123Testing</url>
    <zip>W2 6DX</zip>
</result>
<result>
    <Address></Address>
    <checkin>
        <from>00:30</from>
        <to>01:15</to>
    </checkin>
    <city>London</city>
    <city_id>1</city_id>
    <Location>
        <Latitude>13131313</Latitude>
        <Longitude>11131131</Longitude>
    </Location>
    <city_id>1</city_id>
    <max_persons_in_reservation>1000</max_persons_in_reservation>
    <name>Tahir Hotel</name>
    <ranking>1</ranking>
    <url>123Testing</url>
    <zip>W2 6DX</zip>
</result>

我的XSLT正在生成带有多个根标记的XML。我希望应该有一个根元素,并且在此标记中应该出现多个标记。 我想要这样的结果:

<hotels>
   <result>
    <Address>
    </Address>
    <checkin>
        <from>00:30</from>
        <to>01:15</to>
    </checkin>
    <city>London</city>
    <city_id>1</city_id>
    <Location>
        <Latitude>13131313</Latitude>
        <Longitude>11131131</Longitude>
    </Location>
    <city_id>1</city_id>
    <max_persons_in_reservation>1000</max_persons_in_reservation>
    <name>Tahir Hotel</name>
    <ranking>1</ranking>
    <url>123Testing</url>
    <zip>W2 6DX</zip>
</result>
<result>
    <Address></Address>
    <checkin>
        <from>00:30</from>
        <to>01:15</to>
    </checkin>
    <city>London</city>
    <city_id>1</city_id>
    <Location>
        <Latitude>13131313</Latitude>
        <Longitude>11131131</Longitude>
    </Location>
    <city_id>1</city_id>
    <max_persons_in_reservation>1000</max_persons_in_reservation>
    <name>Tahir Hotel</name>
    <ranking>1</ranking>
    <url>123Testing</url>
    <zip>W2 6DX</zip>
  </result>
</hotels>

请帮我解决此问题。我创建了一个测试页面,您可以通过从此处复制粘贴文本到我的页面来检查您的sugesstions。下面是测试代码的链接:

http://shahbaz.somee.com/Default.aspx

2 个答案:

答案 0 :(得分:1)

我想补充一下:

<xsl:template match="/">
   <hotels>
      <xsl:apply-templates select="result/xml_acc"/>
   </hotels>
</xsl:template>

答案 1 :(得分:0)

Goran提供的答案是正确的,应该修复你的代码。

尽管我将发布我的解决方案重写一些原始代码,因为我认为更符合XSLT理念,更容易维护。但输出略有不同:源文档中未出现的元素不会复制到结果文档(例如地址)和元素输出的顺序。

<?xml version="1.0" encoding="utf-8" ?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:output omit-xml-declaration="yes" indent="yes" method="xml" />
<xsl:strip-space elements="*"/>

<!-- Default template : ignore unrecognized elements and text -->
<xsl:template match="*|text()" />

<!-- Match document root : add hotels element and process each children node of result -->
<xsl:template match="/">
    <hotels>
        <!-- We assume that the XML documents are always going to follow the structure:
             result as the root node and xml_acc elements as its children -->
        <xsl:for-each select="result/xml_acc">
            <result>
                <xsl:apply-templates />
            </result>
        </xsl:for-each>
    </hotels>
</xsl:template>

<!-- Elements to be copied as they are -->
<xsl:template match="Address|city|max_persons_in_reservation|name|ranking|url|zip">
    <xsl:copy-of select="." />
</xsl:template>

<!-- Mapping CityID to city_id-->
<xsl:template match="CityID">
    <city_id>
        <xsl:value-of select="." />
    </city_id>
</xsl:template>

<!-- Elements which need to be joined with sibling elements -->

<xsl:template match="CheckInFrom">
    <checkin>
        <from><xsl:value-of select="." /></from>
        <to><xsl:value-of select="../CheckInTo" /></to>
    </checkin>
</xsl:template>

<xsl:template match="Latitude">
    <Location>
        <xsl:copy-of select=".|../Longitude" />
    </Location>
</xsl:template>

</xsl:stylesheet>