如何通过在XSLT中编写一条语句来避免输出中出现所有名称空间
我写了“排除结果前缀”,甚至在输出中看到了名称空间出现的外观如何通过在XSLT中编写一条语句来避免输出中出现所有名称空间,xslt,Xslt,我写了“排除结果前缀”,甚至在输出中看到了名称空间出现的外观 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:simple="Simple name space" xmlns:xlink="http://www.w3.org/1999/xli
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:simple="Simple name space"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:tcm="http://www.tridion.com/ContentManager/5.0"
xmlns:xh="http://www.w3.org/1999/xhtml"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns:transform-ext="urn:tridion:transform-ext"
xmlns="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="#default simple xh">
如何避免xslt中出现所有名称空间?
排除结果前缀
只需删除结果根标记上的xmlns:foo=”“
属性。标记仍然绑定到同一名称空间。由于标记没有任何匹配的前缀,因此使用默认名称空间
如果希望完全删除名称空间,可以使用以下样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>
<xsl:template match="/|comment()|processing-instruction()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
<stylesheet
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xhtml="http://www.w3.org/1999/xhtml" ...etc >
(来源:)当指定为
“yes”
时,xsl:stylesheet
的exclude result prefixes
属性强制删除LitreReal结果元素(仅)继承的任何命名空间节点,并且不定义文本结果元素的命名空间uri和前缀
Markus Jarderot回答中的以下陈述是错误的:
“排除结果前缀只会删除上的xmlns:foo=”“属性
结果的根标记。“
这里有一个反例:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:z="z:z" exclude-result-prefixes="z">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<z:x xmlns:z="z:z">
<z:y/>
</z:x>
</xsl:template>
</xsl:stylesheet>
<z:x xmlns:z="z:z">
<z:y z:attr="someValue"/>
</z:x>
<x>
<y attr="someValue"/>
</x>
上述转换使用属于“无名称空间”的对应元素或属性替换任何元素或属性。它的一个潜在用途是将具有默认名称空间的文档转换为不具有默认名称空间的文档
例如,当应用于以下源XML文档时:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:z="z:z" exclude-result-prefixes="z">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<z:x xmlns:z="z:z">
<z:y/>
</z:x>
</xsl:template>
</xsl:stylesheet>
<z:x xmlns:z="z:z">
<z:y z:attr="someValue"/>
</z:x>
<x>
<y attr="someValue"/>
</x>
转换的结果是:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:z="z:z" exclude-result-prefixes="z">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<z:x xmlns:z="z:z">
<z:y/>
</z:x>
</xsl:template>
</xsl:stylesheet>
<z:x xmlns:z="z:z">
<z:y z:attr="someValue"/>
</z:x>
<x>
<y attr="someValue"/>
</x>
最后一个警告:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:z="z:z" exclude-result-prefixes="z">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<z:x xmlns:z="z:z">
<z:y/>
</z:x>
</xsl:template>
</xsl:stylesheet>
<z:x xmlns:z="z:z">
<z:y z:attr="someValue"/>
</z:x>
<x>
<y attr="someValue"/>
</x>
如果将此转换应用于包含两个具有相同本地名称但属于两个不同名称空间的元素(或两个属性)的文档,则此转换可能有害。此转换将使用两个元素(或属性)替换这些元素(或属性),这两个元素都属于同一名称空间(无名称空间)。将删除“排除结果前缀”属性(在某些情况下)从输出中删除未使用的命名空间声明。它永远不会删除结果中实际用于元素和属性的命名空间:也就是说,它永远不会更改元素和属性的名称以将它们放在不同的命名空间中(或不放在命名空间中)。如果希望元素不在命名空间中,则在生成它们时必须避免将它们放在命名空间中,无论是使用文字结果元素、xsl:element还是xsl:copy。尝试在样式表中使用此选项:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>
<xsl:template match="/|comment()|processing-instruction()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
<stylesheet
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xhtml="http://www.w3.org/1999/xhtml" ...etc >
我在一些标记中有这样的内容:xmlns=”http://www.w3.org/1999/xhtml
,所以我添加了这两行,现在就可以了
这个源代码帮助了我:当使用XSL 2.0并使用
或
复制元素时,还有非常有用的复制名称空间
属性。将此属性设置为否
在复制元素时不复制名称空间。更多详细信息,请参阅。@EiríkrÚtlendi,是的,这是有用的。不幸的是,这不是一个选项XSLT2.0问题。可以使用此答案(可在之间移植)XSLT 1.0和XSLT 2.0/3.0都是的,这不是针对XSL 1.0的。我遇到了这个问题并找到了您的解决方案,但由于技术限制,我一直在寻找另一种方法,并找到了复制名称空间
属性。为了完整性,我添加了注释,供将来的搜索者使用。