3

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

3 に答える 3

1

XSLT 2.0 標準xpath-default-namespaceのセクション5.2 式とパターンにおける接頭辞なしの QName で説明されているように、属性を追加することにより、XPath 式のデフォルトの名前空間を指定できます。

例:

<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xpath-default-namespace="http://schemas.foo.com/fooNameSpace"> 

    <xsl:template match="FOO[not(@version)]">
        <xsl:copy>
            <xsl:attribute name="version">1</xsl:attribute> 
            <xsl:apply-templates select="node()|@*" /> 
        </xsl:copy>
    </xsl:template>

    <!-- Identity template for copying everything else -->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" /> 
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
于 2010-04-29T08:19:24.700 に答える
0

これは、xsltproc/libxslt を使用して動作します。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://schemas.foo.com/fooNameSpace">
  <xsl:template match="/ns:FOO">
    <xsl:copy>
      <xsl:if test="not(@version)">
        <xsl:attribute name="version">1</xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

以下を生成します。

<?xml version="1.0"?>
<FOO xmlns="http://schemas.foo.com/fooNameSpace" version="1">
    <BAR>
        <Attribute>2067</Attribute>
    </BAR>
</FOO>
于 2010-04-29T04:56:19.483 に答える
0

以下は、XSLT の精神に基づいた 1 つのソリューションです

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fd="http://schemas.foo.com/fooNameSpace"
    >
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*" name="identity">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="/*[self::fd:FOO and not(@version)]">
  <xsl:copy>
    <xsl:attribute name="version">1</xsl:attribute>

    <xsl:call-template name="identity"/>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

この変換が提供された XML ドキュメントに適用されると、必要な正しい結果が生成されます。

<FOO xmlns="http://schemas.foo.com/fooNameSpace"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 version="1">
   <FOO>
      <BAR>
         <Attribute name="HEIGHT">2067</Attribute>
      </BAR>
   </FOO>
</FOO>
于 2010-04-29T04:56:42.260 に答える