0

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

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

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

4

2 回答 2

1

我要补充:

<xsl:template match="/">
   <hotels>
      <xsl:apply-templates select="result/xml_acc"/>
   </hotels>
</xsl:template>
于 2013-02-13T20:54:50.147 回答
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>
于 2013-02-13T22:16:47.223 回答