gpt4 book ai didi

xslt - 从 XML 中排除命名空间

转载 作者:行者123 更新时间:2023-12-04 05:52:36 27 4
gpt4 key购买 nike

我的 xsl 文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:go="http://www.google.com"
exclude-result-prefixes="go">
<xsl:include href="SomeLibrary.xsl"/>

<xsl:template match="/">

<xsl:call-template name="SomeTemplate">
<xsl:with-param name="Element" select="'randomParam'"/>
</xsl:call-template>

</xsl:template>

</xsl:stylesheet>

SomeLibrary.xsl 文件:
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.google.com">
<xsl:template name="SomeTemplate">
<xsl:param name="Element"/>
<Blabla>
<xsl:value-of select="$Element" />
</Blabla>
</xsl:template>
</xsl:stylesheet>

输入 xml:只需使用一个空的 XML。结果是这样的:
<?xml version="1.0" encoding="UTF-8"?>

<Blabla xmlns="http://www.google.com">
randomParam
</Blabla>

我想要的是没有命名空间的“Blabla”节点。如何在不修改我的“SomeLibrary.xsl”的情况下删除它或确保它不会到达那里?

最佳答案

如果您不想编辑导入的样式表代码,删除命名空间的方法是使用两次转换(在 XSLT 1.0 中需要使用 xxx:node-set() 扩展函数):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext" >
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/">
<xsl:variable name="vrtfPass1">
<xsl:call-template name="SomeTemplate">
<xsl:with-param name="Element" select="'randomParam'"/>
</xsl:call-template>
</xsl:variable>

<xsl:variable name="vPass1" select="ext:node-set($vrtfPass1)"/>
<xsl:apply-templates select="$vrtfPass1/node()" mode="pass2"/>
</xsl:template>

<xsl:template match="*" mode="pass2">
<xsl:element name="{name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates mode="pass2"/>
</xsl:element>
</xsl:template>

<xsl:template name="SomeTemplate" xmlns="http://www.google.com">
<xsl:param name="Element"/>
<Blabla>
<xsl:value-of select="$Element" />
</Blabla>
</xsl:template>
</xsl:stylesheet>

当此转换应用于任何 XML 文档(未使用)时,会产生想要的结果(删除默认命名空间) :
<Blabla>randomParam</Blabla>

更新 :

OP 在评论中表示他正在使用 Xalan 2.07。

下面是几乎相同的解决方案,但命名空间和名称为 xxx:node-set() Xalan 中使用的函数:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="xmlns:xalan="http://xml.apache.org/xalan" exclude-result-prefixes="x" >
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/">
<xsl:variable name="vrtfPass1">
<xsl:call-template name="SomeTemplate">
<xsl:with-param name="Element" select="'randomParam'"/>
</xsl:call-template>
</xsl:variable>

<xsl:variable name="vPass1" select="x:nodeset($vrtfPass1)"/>
<xsl:apply-templates select="$vrtfPass1/node()" mode="pass2"/>
</xsl:template>

<xsl:template match="*" mode="pass2">
<xsl:element name="{name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates mode="pass2"/>
</xsl:element>
</xsl:template>

<xsl:template name="SomeTemplate" xmlns="http://www.google.com">
<xsl:param name="Element"/>
<Blabla>
<xsl:value-of select="$Element" />
</Blabla>
</xsl:template>
</xsl:stylesheet>

关于xslt - 从 XML 中排除命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9904294/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com