首页 文章

如何在xslt中删除命名空间

提问于
浏览
0

使用xslt我试图获得xhtml o / p . 我使用过xmlns =“http://www.w3.org/1999/xhtml”

<xsl:stylesheet>

得到xhtml o / p . 每件事都很好,但在第一个div中,我得到了相同的命名空间 . 即

<div  xmlns="http://www.w3.org/1999/xhtml">

现在如何删除xmlns =“http://www.w3.org/1999/xhtml”

2 回答

  • 1

    正如其他人所指出的那样,你可能不想这样做 . 如果您希望输出为XHTML,则需要保留XHTML名称空间声明 .

    话虽这么说,如果你真的想这样做:

    <xsl:stylesheet version="1.0"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <!-- attributes, commments, processing instructions, text: copy as is -->
      <xsl:template match="@*|comment()|processing-instruction()|text()">
        <xsl:copy-of select="."/>
      </xsl:template>
    
      <!-- elements: create a new element with the same name, but no namespace -->
      <xsl:template match="*">
        <xsl:element name="{local-name()}">
          <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
      </xsl:template>
    
    </xsl:stylesheet>
    
  • 4

    为什么要删除命名空间?它是XHTML规范的一部分,你说你想要XHTML输出 . 那么 - 问题出在哪里?

    显然你用 <div> 开始你的输出,否则你将在 <html> 元素上有名称空间声明 .

相关问题