0

我是 xslt 的新手,正在尝试使用 xslt 从 xml 创建 html。

我不知道为什么,但我的 xslt 在只有一个元素时有效,但在多个时无效。

我尝试了各种方法,但正如我所说的那样,我是新手,不知道该怎么做。

这是我的xml

  <?xml version="1.0" encoding="ISO-8859-1"?>
<DestinationResults size="1">
<Destination name="Paris, FRA" order="1">
<HotelAvailabilityListResults size="10">
<Hotel order="1">
<hotelId>188139</hotelId>
<name>
<![CDATA[ Hotel Magenta Paris ]]>
</name>
<address1>38 Boulevard De Magenta</address1>
<address2/>
<address3/>
<city>Paris</city>
<stateProvince/>
<country>FR</country>
<postalCode>75010</postalCode>
<latitude>48.87203</latitude>
<longitude>2.35995</longitude>
<tripAdvisorRating>4.0</tripAdvisorRating>
<fromDate>10/11/2013</fromDate>
<toDate>10/12/2013</toDate>
<shortDescription>
<![CDATA[&lt;p&gt;&lt;b&gt;Property Location&lt;/b&gt; &lt;br /&gt;With a stay at Hotel Magenta Paris in Paris (Gare du Nord - Gare de L&apos;Est), you&apos;ll be minutes from Porte Saint-Martin and close to Louvre Museum. This]]>
</shortDescription>
<thumbNailUrl>
http://origin-images.ian.com/hotels/1000000/890000/889000/888903/888903_22_t.jpg
</thumbNailUrl>
<propertyRating>3.0</propertyRating>
<referenceProximityDistance>-1.0</referenceProximityDistance>
<referenceProximityUnit/>
<HotelProperty>
<promoDescription>Stay 1 nights and save 10% off your stay!</promoDescription>
<ValueAdds>
<valueAdd id="2048">Free Wireless Internet</valueAdd>
</ValueAdds>
<PromoRateInfo>
<displayCurrencyCode>USD</displayCurrencyCode>
<DisplayNightlyRates size="1">
<displayNightlyRate>152.02</displayNightlyRate>
</DisplayNightlyRates>
<displayRoomRate>168.68</displayRoomRate>
<chargeableRoomRateTotal>168.68</chargeableRoomRateTotal>
<chargeableRoomRateTaxesAndFees>16.66</chargeableRoomRateTaxesAndFees>
<taxesOnly>16.66</taxesOnly>
<salesTax>0</salesTax>
<hotelOccupancyTax>0</hotelOccupancyTax>
<nativeCurrencyCode>USD</nativeCurrencyCode>
<NativeNightlyRates size="1">
<nativeNightlyRate>152.02</nativeNightlyRate>
</NativeNightlyRates>
<nativeRoomRate>168.68</nativeRoomRate>
<rateFrequency>B</rateFrequency>
</PromoRateInfo>
<RateInfo>
<displayCurrencyCode>USD</displayCurrencyCode>
<DisplayNightlyRates size="1">
<displayNightlyRate>168.91</displayNightlyRate>
</DisplayNightlyRates>
<displayRoomRate>185.57</displayRoomRate>
<chargeableRoomRateTotal>185.57</chargeableRoomRateTotal>
<chargeableRoomRateTaxesAndFees>16.66</chargeableRoomRateTaxesAndFees>
<taxesOnly>16.66</taxesOnly>
<salesTax>0</salesTax>
<hotelOccupancyTax>0</hotelOccupancyTax>
<nativeCurrencyCode>USD</nativeCurrencyCode>
<NativeNightlyRates size="1">
<nativeNightlyRate>168.91</nativeNightlyRate>
</NativeNightlyRates>
<nativeRoomRate>185.57</nativeRoomRate>
<rateFrequency>B</rateFrequency>
</RateInfo>
</HotelProperty>
<hotelUrl>
http://booking.rated-hotels.co.uk/index.jsp?pageName=hotAvail&cid=409870&hotelID=188139&mode=2&numberOfRooms=1&room-0-adult-total=2&room-0-child-total=0&arrivalMonth=9&arrivalDay=11&departureMonth=9&departureDay=12&showInfo=true&locale=en_US&currencyCode=USD&mcicid=409870.HOTEL.XML.85419
</hotelUrl>
</Hotel>
<Hotel order="2">
<hotelId>188140</hotelId>
<name>
<![CDATA[ Hotel Magenta Paris ]]>
</name>
<address1>38 Boulevard De Magenta</address1>
<address2/>
<address3/>
<city>Paris</city>
<stateProvince/>
<country>FR</country>
<postalCode>75010</postalCode>
<latitude>48.87203</latitude>
<longitude>2.35995</longitude>
<tripAdvisorRating>4.0</tripAdvisorRating>
<fromDate>10/11/2013</fromDate>
<toDate>10/12/2013</toDate>
<shortDescription>
<![CDATA[&lt;p&gt;&lt;b&gt;Property Location&lt;/b&gt; &lt;br /&gt;With a stay at Hotel Magenta Paris in Paris (Gare du Nord - Gare de L&apos;Est), you&apos;ll be minutes from Porte Saint-Martin and close to Louvre Museum. This]]>
</shortDescription>
<thumbNailUrl>
http://origin-images.ian.com/hotels/1000000/890000/889000/888903/888903_22_t.jpg
</thumbNailUrl>
<propertyRating>3.0</propertyRating>
<referenceProximityDistance>-1.0</referenceProximityDistance>
<referenceProximityUnit/>
<HotelProperty>
<promoDescription>Stay 1 nights and save 10% off your stay!</promoDescription>
<ValueAdds>
<valueAdd id="2048">Free Wireless Internet</valueAdd>
</ValueAdds>
<PromoRateInfo>
<displayCurrencyCode>USD</displayCurrencyCode>
<DisplayNightlyRates size="1">
<displayNightlyRate>152.02</displayNightlyRate>
</DisplayNightlyRates>
<displayRoomRate>168.68</displayRoomRate>
<chargeableRoomRateTotal>168.68</chargeableRoomRateTotal>
<chargeableRoomRateTaxesAndFees>16.66</chargeableRoomRateTaxesAndFees>
<taxesOnly>16.66</taxesOnly>
<salesTax>0</salesTax>
<hotelOccupancyTax>0</hotelOccupancyTax>
<nativeCurrencyCode>USD</nativeCurrencyCode>
<NativeNightlyRates size="1">
<nativeNightlyRate>152.02</nativeNightlyRate>
</NativeNightlyRates>
<nativeRoomRate>168.68</nativeRoomRate>
<rateFrequency>B</rateFrequency>
</PromoRateInfo>
<RateInfo>
<displayCurrencyCode>USD</displayCurrencyCode>
<DisplayNightlyRates size="1">
<displayNightlyRate>168.91</displayNightlyRate>
</DisplayNightlyRates>
<displayRoomRate>185.57</displayRoomRate>
<chargeableRoomRateTotal>185.57</chargeableRoomRateTotal>
<chargeableRoomRateTaxesAndFees>16.66</chargeableRoomRateTaxesAndFees>
<taxesOnly>16.66</taxesOnly>
<salesTax>0</salesTax>
<hotelOccupancyTax>0</hotelOccupancyTax>
<nativeCurrencyCode>USD</nativeCurrencyCode>
<NativeNightlyRates size="1">
<nativeNightlyRate>168.91</nativeNightlyRate>
</NativeNightlyRates>
<nativeRoomRate>185.57</nativeRoomRate>
<rateFrequency>B</rateFrequency>
</RateInfo>
</HotelProperty>
<hotelUrl>
http://booking.rated-hotels.co.uk/index.jsp?pageName=hotAvail&cid=409870&hotelID=188139&mode=2&numberOfRooms=1&room-0-adult-total=2&room-0-child-total=0&arrivalMonth=9&arrivalDay=11&departureMonth=9&departureDay=12&showInfo=true&locale=en_US&currencyCode=USD&mcicid=409870.HOTEL.XML.85419
</hotelUrl>
</Hotel>


</HotelAvailabilityListResults>
</Destination>
</DestinationResults>

我的 xslt 是

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
  <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">

<xsl:for-each select="DestinationResults">
<xsl:for-each select="Destination">
<xsl:for-each select="HotelAvailabilityListResults">
<xsl:for-each select="Hotel">





      <div style="background-color:teal;color:white;padding:4px">
        <span style="font-weight:bold"><xsl:value-of select="name"/></span>
        - <xsl:value-of select="city"/>
      </div>
      <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
        <p><xsl:value-of select="shortDescription"/>.
        <span style="font-style:italic">
          <xsl:value-of select="address1"/> 
        </span>.</p>
      </div>
    </xsl:for-each>
 </xsl:for-each>
 </xsl:for-each>
</xsl:for-each>

  </body>
</html>

此 xslt 适用于上述 xml,但是当 xml 中有多个酒店订单(如下所示)时,xslt 不会产生任何结果。

<Hotel order="2">
    <hotelId>188139</hotelId>
    <name>
    <![CDATA[ Hotel Magenta Paris ]]>
    </name>
    <address1>38 Boulevard De Magenta</address1>
    <address2/>
    <address3/>
    <city>Paris</city>
    <stateProvince/>
    <country>FR</country>
    <postalCode>75010</postalCode>
    <latitude>48.87203</latitude>
    <longitude>2.35995</longitude>
    <tripAdvisorRating>4.0</tripAdvisorRating>
    <fromDate>10/11/2013</fromDate>
    <toDate>10/12/2013</toDate>
    <shortDescription>
    <![CDATA[&lt;p&gt;&lt;b&gt;Property Location&lt;/b&gt; &lt;br /&gt;With a stay at Hotel Magenta Paris in Paris (Gare du Nord - Gare de L&apos;Est), you&apos;ll be minutes from Porte Saint-Martin and close to Louvre Museum. This]]>
    </shortDescription>
    <thumbNailUrl>
    http://origin-images.ian.com/hotels/1000000/890000/889000/888903/888903_22_t.jpg
    </thumbNailUrl>
    <propertyRating>3.0</propertyRating>
    <referenceProximityDistance>-1.0</referenceProximityDistance>
    <referenceProximityUnit/>
    <HotelProperty>
    <promoDescription>Stay 1 nights and save 10% off your stay!</promoDescription>
    <ValueAdds>
    <valueAdd id="2048">Free Wireless Internet</valueAdd>
    </ValueAdds>
    <PromoRateInfo>
    <displayCurrencyCode>USD</displayCurrencyCode>
    <DisplayNightlyRates size="1">
    <displayNightlyRate>152.02</displayNightlyRate>
    </DisplayNightlyRates>
    <displayRoomRate>168.68</displayRoomRate>
    <chargeableRoomRateTotal>168.68</chargeableRoomRateTotal>
    <chargeableRoomRateTaxesAndFees>16.66</chargeableRoomRateTaxesAndFees>
    <taxesOnly>16.66</taxesOnly>
    <salesTax>0</salesTax>
    <hotelOccupancyTax>0</hotelOccupancyTax>
    <nativeCurrencyCode>USD</nativeCurrencyCode>
    <NativeNightlyRates size="1">
    <nativeNightlyRate>152.02</nativeNightlyRate>
    </NativeNightlyRates>
    <nativeRoomRate>168.68</nativeRoomRate>
    <rateFrequency>B</rateFrequency>
    </PromoRateInfo>
    <RateInfo>
    <displayCurrencyCode>USD</displayCurrencyCode>
    <DisplayNightlyRates size="1">
    <displayNightlyRate>168.91</displayNightlyRate>
    </DisplayNightlyRates>
    <displayRoomRate>185.57</displayRoomRate>
    <chargeableRoomRateTotal>185.57</chargeableRoomRateTotal>
    <chargeableRoomRateTaxesAndFees>16.66</chargeableRoomRateTaxesAndFees>
    <taxesOnly>16.66</taxesOnly>
    <salesTax>0</salesTax>
    <hotelOccupancyTax>0</hotelOccupancyTax>
    <nativeCurrencyCode>USD</nativeCurrencyCode>
    <NativeNightlyRates size="1">
    <nativeNightlyRate>168.91</nativeNightlyRate>
    </NativeNightlyRates>
    <nativeRoomRate>185.57</nativeRoomRate>
    <rateFrequency>B</rateFrequency>
    </RateInfo>
    </HotelProperty>
    <hotelUrl>
    http://booking.rated-hotels.co.uk/index.jsp?pageName=hotAvail&cid=409870&hotelID=188139&mode=2&numberOfRooms=1&room-0-adult-total=2&room-0-child-total=0&arrivalMonth=9&arrivalDay=11&departureMonth=9&departureDay=12&showInfo=true&locale=en_US&currencyCode=USD&mcicid=409870.HOTEL.XML.85419
    </hotelUrl>
    </Hotel>

任何帮助深表感谢。

问候,杰

4

1 回答 1

0

编写嵌套的 for-each 指令是 XSLT 初学者最常犯的错误。这不是这样做的方法:从使用模板规则开始,并且仅在您对语言足够了解以了解它有用的情况时才使用 for-each。

当然,在这种情况下,您还可以通过使用路径来简化代码:

<xsl:for-each select="DestinationResults/Destination/HotelAvailabilityListResults/Hotel">

像这样的调试问题需要对 XML 树结构有清晰的理解,如果正确缩进 XML,获得这种理解会容易得多。我不确定您所说的“多个酒店订单”是什么意思 - 我在您的第二个示例中只看到一个订单。也许问题是您的第二个示例没有您的样式表假设的 DestinationResults/Destination/HotelAvailabilityListResults 包装器元素;无论如何,如果他们在那里,那么您还没有向他们展示。将代码放在带有 match="Hotel" 的模板规则中可以避免这种对包装器中存在的内容的依赖。

于 2013-09-27T09:47:02.097 回答