0

「FOO」という名前の要素の属性値を変更したいので、次のように記述しました。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match="FOO/@extent">
      <xsl:attribute name="extent">
      <xsl:text>1.5cm</xsl:text>
      </xsl:attribute>
  </xsl:template>

</xsl:stylesheet>

このようなものは動作します。今は同じものが必要ですが、要素は「fs:BOO」です。FOOを「fs:BOO」に置き換えてみましたが、xsltprocはそのようなコードをコンパイルできないと言っています。私はこの問題を次のように一時的に解決します。

sed 's|fs:BOO|fs_BOO|g' | xsltproc stylesheet.xsl - | sed 's|fs_BOO|fs:BOO|g'

しかし、「sed」を使用せずに、もっと簡単な解決策があるのではないでしょうか。

入力データの例:

<root>
    <fs:BOO extent="0mm" />
</root>

書く場合:

<xsl:template match="fs:BOO/@extent">

私が得た:

xsltCompileStepPattern : no namespace bound to prefix fs
compilation error: file test.xsl line 10 element template
xsltCompilePattern : failed to compile 'fs:BOO/@extent'
4

1 に答える 1

1

まず、XMLに名前空間の宣言があると思います。そうしないと、有効になりません。

<root xmlns:fs="www.foo.com">
    <fs:BOO extent="0mm" />
</root>

そして、これはXSLTにも当てはまります。これを実行しようとしている場合は<xsl:template match="fs:BOO/@extent">、XSLTの名前空間への宣言も必要です。

<xsl:stylesheet version="1.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
   xmlns:fs="www.foo.com">

重要なのは、名前空間URIがXMLの名前空間URIと一致することです。

ただし、異なる名前空間に対処したい場合は、別のアプローチを取ることができます。local-name()関数を使用して、名前空間プレフィックスのない要素の名前を確認できます。

このXSLTを試してください

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match="*[local-name()='BOO']/@extent">
      <xsl:attribute name="extent">
         <xsl:text>1.5cm</xsl:text>
      </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

これにより、次のように出力されます

<root xmlns:fs="fs">
   <fs:BOO extent="1.5cm"></fs:BOO>
</root>
于 2012-11-01T08:09:15.333 に答える