[英]How to grab all elements within parent tag and sort with XSLT?
下面是我試圖格式化的 XML 文檔
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="myXSLT_Q9.xsl"?>
<orders>
<order>
<customerid>2384</customerid>
<status>pending</status>
<item instock="Y" itemid="SD93">
<name>Flying By Roller Skates</name>
<price>25.00</price>
<qty>25</qty>
</item>
<item instock="N" itemid="B12">
<name>Bounce-o Ball</name>
<price>.35</price>
<qty>150</qty>
</item>
</order>
<order>
<customerid>5268</customerid>
<status>complete</status>
<item instock="Y" itemid="Q52">
<name>Crash N Burn Skis</name>
<price>20</price>
<qty>10</qty>
</item>
</order>
<order>
<customerid>3384</customerid>
<status>pending</status>
<item instock="Y" itemid="PS93">
<name>All Star Shoe</name>
<price>55.00</price>
<qty>12</qty>
</item>
<item instock="Y" itemid="M12">
<name>All Star Hat</name>
<price>44.35</price>
<qty>15</qty>
</item>
</order>
</orders>
我試圖遍歷所有項目並將每個項目的整個數據放入一個表中,但由於某種原因,我的代碼只抓取了 Item 標簽下的 Name、Price 和 Qty 標簽,而我嘗試過的所有內容都不會抓取 Customerid 和 Status標簽。 排序條件也似乎正確,但是當我添加它時,它停止工作。
<?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>
<head>
<title>Title of the page</title>
</head>
<body>
<table>
<xsl:for-each select="//item">
<xsl:sort select="//item/@itemid">
<xsl:if test="@instock = 'N'">
<tr style="background-color:yellow">
<td><xsl:value-of select="customerid"/></td>
<td><xsl:value-of select="status"/></td>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="qty"/></td>
</tr>
</xsl:if>
<xsl:if test="@instock = 'Y'">
<tr>
<td><xsl:value-of select="customerid"/></td>
<td><xsl:value-of select="status"/></td>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="qty"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我想要的輸出是一個表中所有 Item 訂單的表,按 Itemid 排序,如果 instock = "N" 那么它應該突出顯示該行。 該表的每一行都應該有 Customerid、Status、Item Name、Item Price 和 Item Qty。
在模板或xsl:for-each
使用相對表達式來更改上下文節點:
<xsl:for-each select="//item">
<xsl:sort select="@itemid"/>
...
<td><xsl:value-of select="../customerid"/></td>
我也會縮短
<xsl:if test="@instock = 'N'">
<tr style="background-color:yellow">
<td><xsl:value-of select="customerid"/></td>
<td><xsl:value-of select="status"/></td>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="qty"/></td>
</tr>
</xsl:if>
<xsl:if test="@instock = 'Y'">
<tr>
<td><xsl:value-of select="customerid"/></td>
<td><xsl:value-of select="status"/></td>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="qty"/></td>
</tr>
</xsl:if>
到
<tr>
<xsl:if test="@instock = 'N'">
<xsl:attribute name="style">background-color: yellow;</xsl:attribute>
</xsl:if>
<td><xsl:value-of select="../customerid"/></td>
<td><xsl:value-of select="../status"/></td>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="qty"/></td>
</tr>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.