忙着卡在XSL模板中,我遇到了一个小问题。
使用以下模板代码:
productlisting.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>TFG Product File</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>ProductID</th>
<th>ProductName</th>
</tr>
<xsl:for-each select="ProductFile/Products">
<tr>
<td><xsl:value-of select="ProductFile/Products/Id"/></td>
<td><xsl:value-of select="ProductFile/Products/LocalizedProductList/DisplayName"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>以下是两个产品的XML文件:
productcatalog.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="productlisting.xsl"?>
<ProductFile>
<SiteId>1011</SiteId>
<Products>
<TFGMerchandisingId>251100AAAC0</TFGMerchandisingId>
<Id>251100AAAC0</Id>
<TFGMerchandisingDescription>MELTON COAT TOBACCO</TFGMerchandisingDescription>
<TFGMerchandisingDescriptionLong>MELTON COAT TOBACCO</TFGMerchandisingDescriptionLong>
<Type>ColourSize</Type>
<SubType></SubType>
<VideoUrl></VideoUrl>
<TFGMerchandisingStatus>1</TFGMerchandisingStatus>
<TFGMerchandisingStatusDescription>Open</TFGMerchandisingStatusDescription>
<TFGMerchandisingCreateDate>2017-04-23T16:57:42.983</TFGMerchandisingCreateDate>
<TFGMerchandisingPisDate>2017-03-31T00:00:00</TFGMerchandisingPisDate>
<TFGMerchandisingCommodity>1100</TFGMerchandisingCommodity>
<TFGMerchandisingCommodityDescription>FB SMART JACKETS</TFGMerchandisingCommodityDescription>
<TFGMerchandisingBuyersClass>110001</TFGMerchandisingBuyersClass>
<TFGMerchandisingBuyersClassDescription>OVERCOATS</TFGMerchandisingBuyersClassDescription>
<TFGMerchandisingMaterial>WOOL BLENDS</TFGMerchandisingMaterial>
<TFGMerchandisingWeight>0.00</TFGMerchandisingWeight>
<TFGMerchandisingCubeVolume>0</TFGMerchandisingCubeVolume>
<TFGMerchandisingBrandName>FABIANI</TFGMerchandisingBrandName>
<BrandCode>25</BrandCode>
<CrossSellProductId1></CrossSellProductId1>
<CrossSellProductId2></CrossSellProductId2>
<CrossSellProductId3></CrossSellProductId3>
<DisableCompareOption>false</DisableCompareOption>
<DisableNextDayDelivery>false</DisableNextDayDelivery>
<RequiresRica>false</RequiresRica>
<RequiresId>false</RequiresId>
<RequiresTVLicense>false</RequiresTVLicense>
<RequiresDeposit>false</RequiresDeposit>
<InstallmentAvailable>false</InstallmentAvailable>
<StaffDiscountAllowed>true</StaffDiscountAllowed>
<StaffDiscountOverride>false</StaffDiscountOverride>
<MaxDiscountAllowed>40.00</MaxDiscountAllowed>
<SizeChartIconUrl></SizeChartIconUrl>
<PdfIconUrl></PdfIconUrl>
<IsNavigable>true</IsNavigable>
<TFGMerchandisingTypeCode></TFGMerchandisingTypeCode>
<TFGMerchandisingTypeDescription>FABIANI</TFGMerchandisingTypeDescription>
<Groups>
<GroupSequence>1</GroupSequence>
<GroupId>8c5f3f15-7a57-4186-bd29-3f7f6a3b106f</GroupId>
<LocalizedGroupList>
<Locale>en_ZA</Locale>
<Group>Default</Group>
</LocalizedGroupList>
<Attributes>
<AttributeId>9dd85915-f977-4596-9d35-10d3e23aef1c</AttributeId>
<LocalizedAttributeList>
<Locale>en_ZA</Locale>
<Name>Style Detail</Name>
<Value>Peak lapels Fully lined Single vent Flap pockets</Value>
<TooltipText></TooltipText>
</LocalizedAttributeList>
</Attributes>
<Attributes>
<AttributeId>47fa52e8-44d6-45f3-8249-906434f6e399</AttributeId>
<LocalizedAttributeList>
<Locale>en_ZA</Locale>
<Name>Size & Fit</Name>
<Value>Regular Fit To ensure every Fabiani garment is the perfect fit for you, please refer to our size guide and check our measurements.</Value>
<TooltipText></TooltipText>
</LocalizedAttributeList>
</Attributes>
<Attributes>
<AttributeId>823e6a08-a906-4b2e-9fc1-35c332d263d0</AttributeId>
<LocalizedAttributeList>
<Locale>en_ZA</Locale>
<Name>Fabric</Name>
<Value>Wool blends</Value>
<TooltipText></TooltipText>
</LocalizedAttributeList>
</Attributes>
<Attributes>
<AttributeId>1abbda7a-3f92-4eee-b414-7516c4eb0fe3</AttributeId>
<LocalizedAttributeList>
<Locale>en_ZA</Locale>
<Name>Care</Name>
<Value>Do Not Wash Do Not Bleach Cool Iron Dry-clean Only Do Not Tumble Dry</Value>
<TooltipText></TooltipText>
</LocalizedAttributeList>
</Attributes>
<Attributes>
<AttributeId>479d96a4-f8a7-4048-876a-da9134d03ed1</AttributeId>
<LocalizedAttributeList>
<Locale>en_ZA</Locale>
<Name>Models size</Name>
<Value>Model wearing a large Model measures: chest 39"/ 99cm, waist 32, height 74'02"/ 188cm</Value>
<TooltipText></TooltipText>
</LocalizedAttributeList>
</Attributes>
</Groups>
<LocalizedProductList>
<Locale>en_ZA</Locale>
<DisplayName>MELTON COAT</DisplayName>
<Description>A modern wardrobe essential, our stylish overcoat is expertly crafted into a slim yet unrestrictive silhouette from luxuriously soft Melton, ensuring warmth and comfort on colder days. The single vent allows for freedom of movement while the lining allows it to easily layer over jackets and knitwear.</Description>
<Widget1Url></Widget1Url>
<Widget2Url></Widget2Url>
<Widget3Url></Widget3Url>
<Tabs>
<Name>Product Information</Name>
<Description></Description>
</Tabs>
<SizeChartUrl></SizeChartUrl>
<SizeChartToolTip></SizeChartToolTip>
<PdfUrl></PdfUrl>
<PdfToolTip></PdfToolTip>
<SeoTitle>MELTON COAT</SeoTitle>
</LocalizedProductList>
<Colours>
<TFGMerchandisingStyleId>21303875</TFGMerchandisingStyleId>
<DefaultStyleId>21303875</DefaultStyleId>
<TFGMerchandisingColourId>21303875</TFGMerchandisingColourId>
<TFGMerchandisingColourDescription>LIGHT GREY</TFGMerchandisingColourDescription>
<TFGMerchandisingGeneric>GREY</TFGMerchandisingGeneric>
<ImageMainPrimary>/25/Detail/21303875.jpg</ImageMainPrimary>
<ImageMainAlts>
<URL>/25/DetailAlternative/21303875_01.jpg</URL>
</ImageMainAlts>
<ImageMainAlts>
<URL>/25/DetailAlternative/21303875_02.jpg</URL>
</ImageMainAlts>
<ImageThumbPrimary>/25/Thumbnail/21303875.jpg</ImageThumbPrimary>
<ImageThumbAlts>
<URL>/25/ThumbnailAlternative/21303875_01.jpg</URL>
</ImageThumbAlts>
<ImageThumbAlts>
<URL>/25/ThumbnailAlternative/21303875_02.jpg</URL>
</ImageThumbAlts>
<ImageSearchPrimary>/25/Listing/21303875.jpg</ImageSearchPrimary>
<Swatch>/25/ColourSwatch/21303875_SW.jpg</Swatch>
<DisableEmailMe>false</DisableEmailMe>
<DisableAddToGiftList>false</DisableAddToGiftList>
<DisableAddToWishList>false</DisableAddToWishList>
<DisableAddToCart>false</DisableAddToCart>
<RemoveFromOnlineCatalogue>false</RemoveFromOnlineCatalogue>
<LocalizedColourList>
<Locale>en_ZA</Locale>
<SeoTitle></SeoTitle>
</LocalizedColourList>
<Sizes>
<Sku>21303883</Sku>
<CompanyId>02</CompanyId>
<Sequence>8</Sequence>
<PlanUrl></PlanUrl>
<IsActive>true</IsActive>
<OnSale>false</OnSale>
<StoreStockDisplay>Always</StoreStockDisplay>
<LocalizedSizeList>
<Locale>en_ZA</Locale>
<Description>44 CHEST L</Description>
<SkuDisplayName>44 CHEST L</SkuDisplayName>
</LocalizedSizeList>
</Sizes>
<Sizes>
<Sku>21303885</Sku>
<CompanyId>02</CompanyId>
<Sequence>10</Sequence>
<PlanUrl></PlanUrl>
<IsActive>true</IsActive>
<OnSale>false</OnSale>
<StoreStockDisplay>Always</StoreStockDisplay>
<LocalizedSizeList>
<Locale>en_ZA</Locale>
<Description>46 CHEST L</Description>
<SkuDisplayName>46 CHEST L</SkuDisplayName>
</LocalizedSizeList>
</Sizes>
<Sizes>
<Sku>21303884</Sku>
<CompanyId>02</CompanyId>
<Sequence>9</Sequence>
<PlanUrl></PlanUrl>
<IsActive>true</IsActive>
<OnSale>false</OnSale>
<StoreStockDisplay>Always</StoreStockDisplay>
<LocalizedSizeList>
<Locale>en_ZA</Locale>
<Description>46 CHEST</Description>
<SkuDisplayName>46 CHEST</SkuDisplayName>
</LocalizedSizeList>
</Sizes>
<Sizes>
<Sku>21303876</Sku>
<CompanyId>02</CompanyId>
<Sequence>3</Sequence>
<PlanUrl></PlanUrl>
<IsActive>true</IsActive>
<OnSale>false</OnSale>
<StoreStockDisplay>Always</StoreStockDisplay>
<LocalizedSizeList>
<Locale>en_ZA</Locale>
<Description>36 CHEST</Description>
<SkuDisplayName>36 CHEST</SkuDisplayName>
</LocalizedSizeList>
</Sizes>
<Sizes>
<Sku>21303889</Sku>
<CompanyId>02</CompanyId>
<Sequence>14</Sequence>
<PlanUrl></PlanUrl>
<IsActive>true</IsActive>
<OnSale>false</OnSale>
<StoreStockDisplay>Always</StoreStockDisplay>
<LocalizedSizeList>
<Locale>en_ZA</Locale>
<Description>54 CHEST</Description>
<SkuDisplayName>54 CHEST</SkuDisplayName>
</LocalizedSizeList>
</Sizes>
<Sizes>
<Sku>21303888</Sku>
<CompanyId>02</CompanyId>
<Sequence>13</Sequence>
<PlanUrl></PlanUrl>
<IsActive>true</IsActive>
<OnSale>false</OnSale>
<StoreStockDisplay>Always</StoreStockDisplay>
<LocalizedSizeList>
<Locale>en_ZA</Locale>
<Description>52 CHEST</Description>
<SkuDisplayName>52 CHEST</SkuDisplayName>
</LocalizedSizeList>
</Sizes>
<Sizes>
<Sku>21303882</Sku>
<CompanyId>02</CompanyId>
<Sequence>7</Sequence>
<PlanUrl></PlanUrl>
<IsActive>true</IsActive>
<OnSale>false</OnSale>
<StoreStockDisplay>Always</StoreStockDisplay>
<LocalizedSizeList>
<Locale>en_ZA</Locale>
<Description>44 CHEST</Description>
<SkuDisplayName>44 CHEST</SkuDisplayName>
</LocalizedSizeList>
</Sizes>
<Sizes>
<Sku>21303878</Sku>
<CompanyId>02</CompanyId>
<Sequence>5</Sequence>
<PlanUrl></PlanUrl>
<IsActive>true</IsActive>
<OnSale>false</OnSale>
<StoreStockDisplay>Always</StoreStockDisplay>
<LocalizedSizeList>
<Locale>en_ZA</Locale>
<Description>40 CHEST</Description>
<SkuDisplayName>40 CHEST</SkuDisplayName>
</LocalizedSizeList>
</Sizes>
<Sizes>
<Sku>21303886</Sku>
<CompanyId>02</CompanyId>
<Sequence>11</Sequence>
<PlanUrl></PlanUrl>
<IsActive>true</IsActive>
<OnSale>false</OnSale>
<StoreStockDisplay>Always</StoreStockDisplay>
<LocalizedSizeList>
<Locale>en_ZA</Locale>
<Description>48 CHEST</Description>
<SkuDisplayName>48 CHEST</SkuDisplayName>
</LocalizedSizeList>
</Sizes>
<Sizes>
<Sku>21303880</Sku>
<CompanyId>02</CompanyId>
<Sequence>2</Sequence>
<PlanUrl></PlanUrl>
<IsActive>true</IsActive>
<OnSale>false</OnSale>
<StoreStockDisplay>Always</StoreStockDisplay>
<LocalizedSizeList>
<Locale>en_ZA</Locale>
<Description>None</Description>
<SkuDisplayName>None</SkuDisplayName>
</LocalizedSizeList>
</Sizes>
<Sizes>
<Sku>21303887</Sku>
<CompanyId>02</CompanyId>
<Sequence>12</Sequence>
<PlanUrl></PlanUrl>
<IsActive>true</IsActive>
<OnSale>false</OnSale>
<StoreStockDisplay>Always</StoreStockDisplay>
<LocalizedSizeList>
<Locale>en_ZA</Locale>
<Description>50 CHEST</Description>
<SkuDisplayName>50 CHEST</SkuDisplayName>
</LocalizedSizeList>
</Sizes>
<Sizes>
<Sku>21303881</Sku>
<CompanyId>02</CompanyId>
<Sequence>6</Sequence>
<PlanUrl></PlanUrl>
<IsActive>true</IsActive>
<OnSale>false</OnSale>
<StoreStockDisplay>Always</StoreStockDisplay>
<LocalizedSizeList>
<Locale>en_ZA</Locale>
<Description>42 CHEST L</Description>
<SkuDisplayName>42 CHEST L</SkuDisplayName>
</LocalizedSizeList>
</Sizes>
<Sizes>
<Sku>21303879</Sku>
<CompanyId>02</CompanyId>
<Sequence>1</Sequence>
<PlanUrl></PlanUrl>
<IsActive>true</IsActive>
<OnSale>false</OnSale>
<StoreStockDisplay>Always</StoreStockDisplay>
<LocalizedSizeList>
<Locale>en_ZA</Locale>
<Description>None</Description>
<SkuDisplayName>None</SkuDisplayName>
</LocalizedSizeList>
</Sizes>
<Sizes>
<Sku>21303877</Sku>
<CompanyId>02</CompanyId>
<Sequence>4</Sequence>
<PlanUrl></PlanUrl>
<IsActive>true</IsActive>
<OnSale>false</OnSale>
<StoreStockDisplay>Always</StoreStockDisplay>
<LocalizedSizeList>
<Locale>en_ZA</Locale>
<Description>38 CHEST</Description>
<SkuDisplayName>38 CHEST</SkuDisplayName>
</LocalizedSizeList>
</Sizes>
</Colours>
<ServiceProviderUrl></ServiceProviderUrl>
<RewardCode>66</RewardCode>
<IsDiscountable>true</IsDiscountable>
<BrandImageURL>/25/BrandImage/fabiani.jpg</BrandImageURL>
</Products>
<Products>
<TFGMerchandisingId>194217AAAF6</TFGMerchandisingId>
<Id>194217AAAF6</Id>
<TFGMerchandisingDescription>TH CHASM DUFFEL L GREY</TFGMerchandisingDescription>
<TFGMerchandisingDescriptionLong>TH CHASM DUFFEL L GREY</TFGMerchandisingDescriptionLong>
<Type>ColourNoSize</Type>
<SubType></SubType>
<VideoUrl></VideoUrl>
<TFGMerchandisingStatus>2</TFGMerchandisingStatus>
<TFGMerchandisingStatusDescription>On Clearance</TFGMerchandisingStatusDescription>
<TFGMerchandisingCreateDate>2017-03-28T07:36:58.567</TFGMerchandisingCreateDate>
<TFGMerchandisingPisDate>2015-11-04T14:59:54.313</TFGMerchandisingPisDate>
<TFGMerchandisingCommodity>4217</TFGMerchandisingCommodity>
<TFGMerchandisingCommodityDescription>Duffel Bags</TFGMerchandisingCommodityDescription>
<TFGMerchandisingBuyersClass>421702</TFGMerchandisingBuyersClass>
<TFGMerchandisingBuyersClassDescription>Large</TFGMerchandisingBuyersClassDescription>
<TFGMerchandisingMaterial>TPE Laminate</TFGMerchandisingMaterial>
<TFGMerchandisingWeight>0.00</TFGMerchandisingWeight>
<TFGMerchandisingCubeVolume>0</TFGMerchandisingCubeVolume>
<TFGMerchandisingBrandName>Thule</TFGMerchandisingBrandName>
<BrandCode>19</BrandCode>
<CrossSellProductId1></CrossSellProductId1>
<CrossSellProductId2></CrossSellProductId2>
<CrossSellProductId3></CrossSellProductId3>
<DisableCompareOption>false</DisableCompareOption>
<DisableNextDayDelivery>false</DisableNextDayDelivery>
<RequiresRica>false</RequiresRica>
<RequiresId>false</RequiresId>
<RequiresTVLicense>false</RequiresTVLicense>
<RequiresDeposit>false</RequiresDeposit>
<InstallmentAvailable>false</InstallmentAvailable>
<StaffDiscountAllowed>true</StaffDiscountAllowed>
<StaffDiscountOverride>false</StaffDiscountOverride>
<MaxDiscountAllowed>40.00</MaxDiscountAllowed>
<SizeChartIconUrl></SizeChartIconUrl>
<PdfIconUrl></PdfIconUrl>
<IsNavigable>true</IsNavigable>
<TFGMerchandisingTypeCode></TFGMerchandisingTypeCode>
<TFGMerchandisingTypeDescription></TFGMerchandisingTypeDescription>
<Groups>
<GroupSequence>1</GroupSequence>
<GroupId>a9c1c190-5882-4e41-85a7-e0b14096f786</GroupId>
<LocalizedGroupList>
<Locale>en_ZA</Locale>
<Group>Default</Group>
</LocalizedGroupList>
<Attributes>
<AttributeId>e07ef650-9719-4022-b0c0-c2837f04c856</AttributeId>
<LocalizedAttributeList>
<Locale>en_ZA</Locale>
<Name>End Use</Name>
<Value>Traveller</Value>
<TooltipText></TooltipText>
</LocalizedAttributeList>
</Attributes>
<Attributes>
<AttributeId>20b2e363-3364-4d54-90ce-51b26edebc2b</AttributeId>
<LocalizedAttributeList>
<Locale>en_ZA</Locale>
<Name>Fabrication</Name>
<Value>TPE Laminate</Value>
<TooltipText></TooltipText>
</LocalizedAttributeList>
</Attributes>
<Attributes>
<AttributeId>35da6c1c-bd22-4858-a1d4-33508456d13c</AttributeId>
<LocalizedAttributeList>
<Locale>en_ZA</Locale>
<Name>Capacity</Name>
<Value>90 l</Value>
<TooltipText></TooltipText>
</LocalizedAttributeList>
</Attributes>
<Attributes>
<AttributeId>b970e2f9-b7ff-407b-9b3b-2a82eab95e99</AttributeId>
<LocalizedAttributeList>
<Locale>en_ZA</Locale>
<Name>Weight</Name>
<Value>1.2 kg</Value>
<TooltipText></TooltipText>
</LocalizedAttributeList>
</Attributes>
<Attributes>
<AttributeId>468d3dc8-16e8-4327-85c7-e118d25c82cd</AttributeId>
<LocalizedAttributeList>
<Locale>en_ZA</Locale>
<Name>Dimensions</Name>
<Value>External height 33 cm
External length 74 cm
External depth 42 cm</Value>
<TooltipText></TooltipText>
</LocalizedAttributeList>
</Attributes>
<Attributes>
<AttributeId>996f1e59-97bc-4f99-8695-89c4e49c4f9c</AttributeId>
<LocalizedAttributeList>
<Locale>en_ZA</Locale>
<Name>Features</Name>
<Value>Oversized, wide mouth opening make it easy to pack and easy to get to your gear.
Side access makes it easy to reach the main compartment from any angle
Duffel straps stow cleanly along the side of the duffel.
Straps quickly convert the bag from a backpack to a duffel.
Durable, water proof tarpaulin fabric stands up on its own for easy packing but easily folds for storage.
Internal mesh pockets keep your gear organized.
External compression straps prevent bag contents from falling to the bottom of the bag when in backpack mode.
Padded bottom cushions your gear from the ground.
Locking zippers deter thieves (lock sold separately).
Exterior stash pocket stores small items.</Value>
<TooltipText></TooltipText>
</LocalizedAttributeList>
</Attributes>
</Groups>
<LocalizedProductList>
<Locale>en_ZA</Locale>
<DisplayName>Thule Chasm Duffel Large-Blue</DisplayName>
<Description>Thule Chasm Duffel Large in Blue.With a wide mouth opening and removable backpack straps these rugged, weather resistant duffels are your go-to gear hauler for any adventure.</Description>
<Widget1Url></Widget1Url>
<Widget2Url></Widget2Url>
<Widget3Url></Widget3Url>
<Tabs>
<Name>Product Information</Name>
<Description></Description>
</Tabs>
<SizeChartUrl></SizeChartUrl>
<SizeChartToolTip></SizeChartToolTip>
<PdfUrl></PdfUrl>
<PdfToolTip></PdfToolTip>
<SeoTitle>Thule Chasm Duffel Large-Blue</SeoTitle>
</LocalizedProductList>
<ServiceProviderUrl></ServiceProviderUrl>
<RewardCode>94</RewardCode>
<IsDiscountable>true</IsDiscountable>
<BrandImageURL>/19/BrandImage/thu.jpg</BrandImageURL>
<Activity>Traveller</Activity>
</Products>
</ProductFile>但我得到了以下结果。

当我删除for-每条语句时,它可以正常工作。有没有读到有人说,每个方法都有点混乱,http://gregbee.ch/blog/using-xsl-for-each-is-almost-always-wrong,我是不是漏掉了什么?
发布于 2017-05-08 12:31:44
使用xsl:for-each没有任何问题。你的方法的问题是:
<xsl:for-each select="ProductFile/Products">将您置于Products的上下文中。从这里开始,指示:
<xsl:value-of select="ProductFile/Products/Id"/>不选择任何东西,因为ProductFile不是Products的子代。您需要使用Id的相对路径,这是简单的:
<xsl:value-of select="Id"/>同样,您需要更改:
<xsl:value-of select="ProductFile/Products/LocalizedProductList/DisplayName"/>至:
<xsl:value-of select="LocalizedProductList/DisplayName"/>发布于 2017-05-08 12:23:19
<xsl:for-each select="ProductFile/Products">
<tr>
<td><xsl:value-of select="Id"/></td>
<td><xsl:value-of select="LocalizedProductList/DisplayName"/></td>
</tr>
</xsl:for-each>发布于 2017-05-08 12:21:29
你可以用这个:
<xsl:for-each select="ProductFile/Products">
<tr>
<td><xsl:value-of select="Id"/></td>
<td><xsl:value-of select="LocalizedProductList/DisplayName"/></td>
</tr>
</xsl:for-each>https://stackoverflow.com/questions/43847467
复制相似问题