【发布时间】:2015-04-24 06:44:36
【问题描述】:
我是 XSLT 转换的新手。我的输出 xml 中有命名空间映射问题。
输入的 XML 是
<m:class xmlns:m="http://www.NotRequirednamespace.com">
<queryDetails>hello</queryDetails>
</m:class>
我的 XSLT 是这样的
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://www.neededNamespace.com" xmlns:t="http://www.NotRequirednamespace.com" exclude-result-prefixes="t">
<xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes"/>
<!-- Stylesheet to remove all namespaces from a document -->
<!-- NOTE: this will lead to attribute name clash, if an element contains
two attributes with same local name but different namespace prefix -->
<!-- Nodes that cannot have a namespace are copied as such -->
<xsl:template match="/">
<school xmlns="http://www.neededNamespace.com">
<xsl:apply-templates/>
</school>
</xsl:template>
<!-- template to copy elements -->
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<!-- template to copy attributes -->
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<!-- template to copy the rest of the nodes -->
<xsl:template match="comment() | text() | processing-instruction()">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>
输出的 XML 是
<school xmlns="http://www.neededNamespace.com">
<class xmlns="">
<queryDetails>hello</queryDetails>
</class>
</school>
但我不想要元素类中的名称默认命名空间 (xmlns="")。我喜欢将类元素的默认命名空间指定为“http://www.neededNamespace.com”。所以我需要输出xml如下。
<school xmlns="http://www.neededNamespace.com">
<class>
<queryDetails>hello</queryDetails>
</class>
</school>
我已经尝试了所有我知道的选项。你能帮忙吗?提前致谢。
【问题讨论】:
-
搜索“XSLT 默认命名空间”,找到其他 666 位落入同一大象陷阱的人。
标签: xslt