ルート要素「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>