首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Saxon更改XSLT属性命名空间

用Saxon更改XSLT属性命名空间
EN

Stack Overflow用户
提问于 2016-06-01 16:35:25
回答 1查看 500关注 0票数 2

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

从输入XML:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<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正在正确地更改命名空间,但属性命名空间除外:

代码语言:javascript
复制
  <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接收:

代码语言:javascript
复制
<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>

我如何告诉Saxon9.3.0.5去掉属性meta:name的旧属性命名空间?

如何删除xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0“?

如果有人可以帮忙,提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-01 17:08:13

我会这样做:

代码语言:javascript
复制
<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>

这将返回:

代码语言:javascript
复制
<?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>

我相信这和你预期的产量是一样的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37574264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档