XSLT root tag namespace instead of element attribute namespace

房东的猫 提交于 2019-12-03 13:21:52

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>

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.

Perhaps I misunderstand the question, but I think you should do this:

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

<xsl:output method="xml" omit-xml-declaration="no" standalone="yes"  indent="yes"/>

<xsl:template match="myMatchedNode">
  <tag>Some text i want inserted into the xsl</mynamespace>

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

</xsl:stylesheet>

That should allow you to use your namespace tags as the default namespace and the output document will have them as the default namespace as well. Your input document can still use the nasty mynamespace:tag syntax.

You could try doing this, too:

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

I'm not entirely sure that will replace mynamespace:tag with just tag though, and if it does, it may be implementation dependent.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!