How to get namespace prefix as defined in stylesheet and not from input XML

泄露秘密 提交于 2019-12-11 19:18:00

问题


I have this input XML:

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

and I am using the following XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:y="http://www.w3.org/1999/xhtml" version="1.0">
<xsl:output method="text" indent="yes"/>
<xsl:template match="*">
    <xsl:value-of select="name()"/>
</xsl:template>
</xsl:stylesheet>

The output I am getting is (root element with prefix x, as defined in input XML)

x:html

The output I expect is (root element with prefix y, as defined in the XSLT):

y:html

回答1:


You could look on the namepace axis of the root element of your XSLT code e.g.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:y="http://www.w3.org/1999/xhtml" version="1.0">
<xsl:output method="text" indent="yes"/>
<xsl:variable name="xslt-root" select="document('')/*"/>
<xsl:template match="*">
  <xsl:value-of select="concat(local-name($xslt-root/namespace::*[. = namespace-uri(current())]), ':', local-name())"/>
</xsl:template>
</xsl:stylesheet>


来源:https://stackoverflow.com/questions/21811344/how-to-get-namespace-prefix-as-defined-in-stylesheet-and-not-from-input-xml

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