XSLT 根标记命名空间而不是元素属性命名空间 [英] XSLT root tag namespace instead of element attribute namespace

查看:25
本文介绍了XSLT 根标记命名空间而不是元素属性命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XSL 文件来传输另一个 XSL 文件.我希望命名空间声明在根标签上,而不是在每个元素上重复!!

I have a XSL file to transfer another XSL file. I want the namespace declaration to be on the root tag, instead of it being repeated on every single element!!

这是我的样式表:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mynamespace="somenamespace" version="2.0">
    <xsl:output method="xml" omit-xml-declaration="no" standalone="yes" indent="yes" />
    <xsl:template match="myMatchedNode">
        <mynamespace:tag>Some text i want inserted into the xsl</mynamespace:tag>
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" /></xsl:copy>
    </xsl:template>
</xsl:stylesheet>

它输出如下:

....

<mynamespace:tag xmlns:mynamespace="somenamespace">Some text i want inserted into the xsl</mynamespace:tag>

....

如何将命名空间声明强制添加到结果的根标记上?!

How do i force the namespace declaration onto the root tag of the result?!

推荐答案

您需要将命名空间节点插入到结果树的文档元素中.有几种方法可以做到这一点,这取决于您使用的是 XSLT 1.0 还是 2.0.以下是2.0解决方案.我假设您正在对输入文档进行修改后的身份转换(您的问题并未真正指定):

You need to insert the namespace node onto the document element of your result tree. There are several ways to do this, depending on whether you're using XSLT 1.0 or 2.0. The following is a 2.0 solution. I'm assuming that you're doing a modified identity transform on the input document (your question didn't really specify):

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- special rule for the document element -->
  <xsl:template match="/*">
    <xsl:copy>
      <!-- Add a namespace node -->
      <xsl:namespace name="mynamespace" select="'somenamespace'"/>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

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

  <!-- the rest of your rules -->

</xsl:stylesheet>

有关在输出文档中控制命名空间的所有不同技术的完整介绍,请查看我网站上XSLT 中的命名空间"文章的命名空间不足" 部分.

For complete coverage of all the different techniques for controlling namespaces in your output document, check out the "Not enough namespaces" section of the "Namespaces in XSLT" article on my website.

这篇关于XSLT 根标记命名空间而不是元素属性命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆