我有一個結構如下的 XML 檔案:
<Products>
<Product>
<sku>1234567</sku>
<attribute:pa_brand xmlns:attribute="attribute">bugatti</attribute:pa_brand>
<attribute_data:pa_brand xmlns:attribute_data="attribute_data">5|1|0</attribute_data:pa_brand>
</Product>
</Products>
我正在嘗試從某個品牌中選擇所有產品。我嘗試了以下 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<Products>
<xsl:apply-templates select="//Product[attribute:pa_brand = 'bugatti']"/>
</Products>
</xsl:template>
<xsl:template match="Product">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
在 Mac OS 上使用 XML Starlet 它給了我:無法評估“選擇”運算式。
向節點名稱添加單引號: select="//Product['attribute:pa_brand' = 'bugatti']"/> 運行查詢,但不回傳任何結果。
在選擇中使用一個簡單的節點,即: 'sku' 像這樣://Product[sku='123456'] 作業正常。我什至不知道這個符號叫什么<foo:bar></foo:bar>。我不知道節點名稱的“bar”部分是如何呼叫的。嘗試過 W3CSchools 和各種參考資料。我發現的所有示例和參考資料都只描述了簡單的節點,或者具有屬性<foo></foo>或<foo bar='baz'></foo>. 找不到任何<foo:bar>baz</foo:bar>參考。
uj5u.com熱心網友回復:
怎么樣:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:attribute="attribute">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Products">
<xsl:copy>
<xsl:copy-of select="Product[attribute:pa_brand = 'bugatti']"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
請注意xsl:stylesheet元素中的命名空間宣告。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/373584.html
