我有以下的XML代碼(這是我正在嘗試使用的代碼片段)。For循環For循環和If語句
<products>
<product>
<productname>Name2</productname>
<productsize measurement="ml">250</productsize>
<productsize measurement="ml">500</productsize>
<productsize measurement="ml">750</productsize>
<otherelements></otherelements>
<price size="250" packsize="1">1.25</price>
<price size="250" packsize="6">7.50</price>
<price size="250" packsize="12">7.00</price>
<price size="500" packsize="1">1.75</price>
<price size="500" packsize="6">10.50</price>
<price size="500" packsize="12">19.00</price>
<price size="750" packsize="1">2.25</price>
<price size="750" packsize="6">13.50</price>
<price size="750" packsize="12">25.00</price>
</product>
<product>
<productname>Name1</productname>
<productsize measurement="ml">250</productsize>
<productsize measurement="ml">750</productsize>
<otherelements></otherelements>
<price size="250" packsize="1">1.25</price>
<price size="250" packsize="6">7.50</price>
<price size="250" packsize="12">7.00</price>
<price size="750" packsize="1">2.25</price>
<price size="750" packsize="6">13.50</price>
<price size="750" packsize="12">25.00</price>
</product>
</products>
我所試圖做的是這樣的
250ml 1=£1.25, 6=£7.50, 12=£7.00
500ml 1=£1.75, 6=£10.50, 12=£19.00
750ml 1=£2.25, 6=£13.50, 12=£25.00
顯示它,但它沒有工作,這裏是我的XSL代碼什麼,我做錯了,我知道它有與內做for循環。
<xsl:for-each select="x:productsize">
<p>
<xsl:value-of select="."/>
<xsl:value-of select="@measurement"/>
</p>
<xsl:for-each select="x:price">
<xsl:if test="productsize = '@size'">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
你得到了什麼錯誤?你有一個聲明的命名空間(如果你這樣做,請發佈完整的源代碼和xslt)。在什麼情況下你的每一個? (請發佈完整的xsl) – helderdarocha
似乎在源XML中會有多個產品;如果是這樣,每個產品是否有唯一的標識符? –
是的,所有完成的命名空間,正如** helderdarocha **向我展示的。道歉,但很多是缺少和頁面顯示,但我沒有得到任何數據。我知道這與** freefaller **解釋過的嵌套問題有關。 – Dino