2013-02-13 136 views
0

我的XML文件:XSLT无法正常转化

<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

回答

1

我想补充:

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

它不工作,当我在我的XSLT文件中添加以下代码,结果如下图所示: user2069823 2013-02-13 21:06:07

+0

对不起 - 看到更新 – Goran 2013-02-13 21:11:44

0

是戈兰提供的答案是正确的,这应该解决您的代码。

尽管我打算发布我的解决方案,重写了一些原始代码,因为我认为更符合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>