2009-07-02 15 views
18

を削除します。XSL - コピー要素が、私はこのように、属性のみに使用される名前空間を宣言し、いくつかのXMLを持っている未使用の名前空間(S)

<?xml version="1.0" encoding="UTF-8"?> 
<a xmlns:x="http://tempuri.com"> 
    <b> 
     <c x:att="true"/> 
     <d>hello</d> 
    </b> 
</a> 

私はコピーを作成するために、XSLを使用したいです選択されたノードの数とその値 - 属性を取り除く。だから私の所望の出力は次のようになります。

<?xml version="1.0" encoding="UTF-8"?> 
<b> 
    <c /> 
    <d>hello</d> 
</b> 

私はほとんどこれを行い、いくつかのXSLを持っているが、私はそれは、出力のトップレベルの要素に名前空間宣言を入れて停止しているように見えることはできません。私のXSLは次のとおりです。

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="/"> 
     <xsl:apply-templates select="https://stackoverflow.com/a/b"/> 
    </xsl:template> 

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

出力の最初の要素は<b xmlns:x="http://tempuri.com">の代わり<b>です。私はXSLのネームスペースを宣言して、プレフィックスをexclude-result-prefixesリストに入れようとしましたが、これは何の効果もないようです。私は間違って何をしていますか?

更新:私は、XSLのネームスペースを宣言し、extension-element-prefixesという属性を使用していることがわかりましたが、これは正しくないようです。私はこれを使うことができると思いますが、なぜexclude-result-prefixesが動作しないのか知りたいのですが!

更新:実際には、extension-element-prefixesソリューションはMSXMLではなくXMLSpyのビルトインXSLTエンジンでのみ動作するようです。

答えて

9
<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
    xmlns:x="http://tempuri.com"> 
    <xsl:template match="/"> 
     <xsl:apply-templates select="https://stackoverflow.com/a/b"/> 
    </xsl:template> 

    <xsl:template match="*"> 
     <xsl:element name="{local-name(.)}"> 
      <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="@*"> 
     <xsl:copy/> 
    </xsl:template> 

    <!-- This empty template is not needed. 
Neither is the xmlns declaration above: 
    <xsl:template match="@x:*"/> --> 
</xsl:stylesheet> 

私は説明を見つけましたhere

マイケル・ケイは書きました:
は除外し、その結果、接頭辞のみリテラル結果要素によって スタイルシートからコピーされた名前空間に影響を与え、それがソース文書から 名前空間のコピーには影響を与えません。

+0

おかげで、(これは(「@ *」テンプレートが必要とされていないようですが)作業を行いますが、私は、「ローカル名を考えました) "機能はかなり遅かった..? –

+1

XSLT 2.0が欲しいです! – Riri

+0

ほぼ完璧ですが、出力にx:att属性がありません。 "@ *"テンプレートは呼び出されません。 の直前に、を追加してみてください。 –

5
<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:x="http://tempuri.com" 
    exclude-result-prefixes="x" 
> 

    <!-- the identity template copies everything 1:1 --> 
    <xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()" /> 
    </xsl:copy> 
    </xsl:template> 

    <!-- this template explicitly cares for namespace'd attributes --> 
    <xsl:template match="@x:*"> 
    <xsl:attribute name="{local-name()}"> 
     <xsl:value-of select="." /> 
    </xsl:attribute> 
    </xsl:template> 

</xsl:stylesheet> 
2

これは出力からのxの名前空間を削除します。

<xsl:namespace-alias result-prefix="#default" stylesheet-prefix="x" /> 

デフォルトの名前空間を扱うときは、2つのことを忘れないでください。まずスタイルシートタグ内の何かにマップしてから、namespace-aliasを使用してスタイルシートタグを削除します。

4

(属性copy-namespaces='no'を注意してください)、これを試してみてください:

<xsl:template match="node()"> 
    <xsl:copy copy-namespaces="no"> 
      <xsl:apply-templates select="node()"/> 
    </xsl:copy> 
</xsl:template> 
関連する問題