xml - XSLT 使用 Saxon 更改属性命名空间

标签 xml xslt namespaces saxon

我尝试使用 XSLT 来更改标签/属性命名空间。

从输入 XML :

<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: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>

使用此 XSL 接收:
<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>

我怎样才能告诉 Saxon 9.3.0.5 去掉属性 meta:name 的旧属性命名空间?

如何删除 xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.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>

我相信这与您的预期输出相同。

关于xml - XSLT 使用 Saxon 更改属性命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37574264/

相关文章:

java - 如何从 JAXBElement<Object> 获取对象

javascript - Ajax:未从 XML 文件中检索数据

xslt - 创建用于添加序列的递归字符串函数

c++ - 在没有 C++14 的情况下定义模板常量的正确方法?

Javascript 命名空间就绪函数

javascript - JavaScript 命名空间、类和继承的简单示例

python - 将Python win32evtlog对象转换为xml

java - 在 XSD 中声明不同类型的集合

javascript - 我如何使用 XML 设计样式

javascript - 如何在 html 属性内插入带有 javascript 属性的 XSLT 元素?