2016-06-01 7 views
2

XSLTを使用してタグ/属性の名前空間を変更しようとしました。入力XMLからXSLT Saxonで属性名前空間を変更

<office:document-meta 
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" 
xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"> 
    <office:meta> 
    <meta:user-defined meta:name="Info 1">in</meta:user-defined> 
    </office:meta> 
</office:document-meta> 

私は、出力XMLをしたい:

<office:document 
    xmlns:office="http://openoffice.org/2000/office" 
    xmlns:meta="http://openoffice.org/2000/meta"> 
    <office:meta> 
    <meta:user-defined meta:name="Info 1">in</meta:user-defined> 
    </office:meta> 
</office:document> 

私のXSLTは正しく、属性の名前空間を除いて、名前空間を変更している。このXSLで

<xsl:stylesheet 
    xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" 
    xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="2.0"> 
    <xsl:output method="xml" indent="yes"/> 
    <!-- Match root and output in new namespace --> 
    <xsl:template match="office:document-meta/office:meta" > 
     <office:document 
     xmlns:office="http://openoffice.org/2000/office" 
     xmlns:meta="http://openoffice.org/2000/meta" > 
     <office:meta> 
      <xsl:apply-templates select="@*|node()"/> 
     </office:meta> 
     </office:document> 
    </xsl:template> 
    <!-- 
     Expected <meta:user-defined meta:name="Info 1">in</meta:user-defined> 
     Received <meta:user-defined xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" meta:name="Info 1">in</meta:user-defined> 
    --> 
    <xsl:template match="meta:user-defined"> 
     <xsl:copy copy-namespaces="no" > 
      <xsl:attribute name="meta:name"><xsl:value-of select="@*"/></xsl:attribute> 
      <xsl:apply-templates select="node()"/> 
     </xsl:copy> 
    </xsl:template> 
    </xsl:stylesheet> 

を受け取ります:

<office:document 
    xmlns:office="http://openoffice.org/2000/office" 
    xmlns:meta="http://openoffice.org/2000/meta"> 
    <office:meta> 
    <meta:user-defined 
     xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" 
     meta:name="Info 1">in</meta:user-defined> 
    </office:meta> 
</office:document> 

属性meta:nameの古い属性名前空間を取り除くようにSaxon 9.3.0.5に指示する方法を教えてください。

xmlns:meta = "urn:oasis:names:tc:opendocument:xmlns:meta:1.0"を削除するにはどうすればよいですか?

誰でも事前にお礼をお寄せください!

+0

あなたのスタイルシートはあなたが主張する結果を生成しません**。 –

答えて

0

私は、このようにそれを行うだろう:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:office1="urn:oasis:names:tc:opendocument:xmlns:office:1.0" 
xmlns:meta1="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" 
xmlns:office="http://openoffice.org/2000/office" 
xmlns:meta="http://openoffice.org/2000/meta" 
exclude-result-prefixes="office1 meta1"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="office1:document-meta"> 
    <office:document> 
     <xsl:apply-templates select="@*|node()"/> 
    </office:document> 
</xsl:template> 

<xsl:template match="office1:*"> 
    <xsl:element name="office:{local-name()}"> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="meta1:*"> 
    <xsl:element name="meta:{local-name()}"> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="@office1:*"> 
    <xsl:attribute name="office:{local-name()}" select="."/> 
</xsl:template> 

<xsl:template match="@meta1:*"> 
    <xsl:attribute name="meta:{local-name()}" select="."/> 
</xsl:template> 

</xsl:stylesheet> 

これが返されます:私はあなたの予想される出力と同じであると考えてい

<?xml version="1.0" encoding="UTF-8"?> 
<office:document xmlns:office="http://openoffice.org/2000/office" 
       xmlns:meta="http://openoffice.org/2000/meta"> 
    <office:meta> 
     <meta:user-defined meta:name="Info 1">in</meta:user-defined> 
    </office:meta> 
</office:document> 

を。

+0

ありがとうございます! 名前空間を区別するために接頭辞の名前を変更することを考えるべきでした。 同じプレフィックスにもかかわらず、名前空間は異なります。 – RemiKoutcherawy

関連する問題