【发布时间】:2022-01-10 20:53:46
【问题描述】:
我有一个结构如下的 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>。我不知道如何调用节点名称的“条”部分。尝试过 W3CSchools 和各种参考资料。我发现的所有示例和参考资料都只描述了简单的节点,或者具有<foo></foo> 或<foo bar='baz'></foo> 属性的节点。找不到任何 <foo:bar>baz</foo:bar> 参考。
【问题讨论】:
-
不确定您使用什么作为测试环境。您的样式表应该会产生错误,因为
attribute前缀未绑定到命名空间。 -
“我什至不知道这个符号叫什么”。任何有关 XML 的书都会有关于名称空间的章节。您需要重新评估学习技术的方法:在遇到编码问题之前不要放弃对基本概念的研究。在编写一行代码之前,您需要大致了解自己在做什么。