我有一个包含以下内容的 XML 文件:
<contents id="MLC1" name="Requirement1" uri="C:\abc.txt" xsi:type="requirement:Requirement" type="">
<contents id="GO1" name="Goal1" uri="C:\abc.txt" xsi:type="goal:Goal">
我正在尝试匹配我的 XML 文件中的所有元素,该文件具有属性 xsi:type="requirement:Requirement"
以便我可以向其添加一个名为“label”的新属性.这是我的样式表:
<xsl:template match="//contents[@type='requirement:Requirement']">
<contents>
<xsl:attribute name="label">
<xsl:value-of select="@name"/>
</xsl:attribute>
<xsl:apply-templates select="node()|@*"/>
</contents>
</xsl:template>
我已经在我的样式表中声明了 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
,但它似乎无法匹配任何东西。我怀疑这是因为原始 xml 中还有另一个属性“类型”,它没有 xsi 命名空间。有没有人有任何建议我应该使用什么来正确匹配这个元素?
最佳答案
通常,您需要使用命名空间前缀来正确选择属性:
<xsl:template match="//contents[@xsi:type='requirement:Requirement']">
如果您使用的是模式感知 XSLT 处理器,则 xsi:type
属性可能具有特殊含义,需要进行相应处理。 See here for more info ,但本质上你需要做的是:
<xsl:template match='//contents[@xsi:type =
QName("http://requirement/namespace/url/goes/here/", "Requirement")]'>
关于xml - 将元素与 xsi :type attribute 匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27678727/