我想匹配一个根元素“FOO”并对其执行转换(添加版本属性),其余部分保持不变。到目前为止,我的转换如下所示:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://schemas.foo.com/fooNameSpace">
<xsl:template match="//FOO">
<xsl:choose>
<xsl:when test="@version">
<xsl:apply-templates select="node()|@*" />
</xsl:when>
<xsl:otherwise>
<FOO>
<xsl:attribute name="version">1</xsl:attribute>
<xsl:apply-templates select="node()|@*" />
</FOO>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
但是,这不会执行任何转换。它甚至没有检测到元素。所以我需要添加命名空间以使其工作:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fd="http://schemas.foo.com/fooNameSpace">
<xsl:template match="//fd:FOO">
…
但这会将命名空间属性附加到 FOO 元素以及其他元素:
<FOO xmlns:fd="http://schemas.foo.com/fooNameSpace" version="1" id="fooid">
<BAR xmlns="http://schemas.foo.com/fooNameSpace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- 有没有办法说元素正在使用默认命名空间?
- 我们可以在默认名称空间中匹配和添加元素吗?
这是原始的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<FOO xmlns="http://schemas.foo.com/fooNameSpace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<BAR>
<Attribute name="HEIGHT">2067</Attribute>
</BAR>
</FOO>