gpt4 book ai didi

xslt - xsl 生成带前缀的属性

转载 作者:行者123 更新时间:2023-12-04 16:59:21 24 4
gpt4 key购买 nike

我对 XSL 完全陌生,并且负责生成带有 xmlns:A.B.C 前缀/值作为根节点属性的输出 xml。具体是这样的:

<GetVersionResponse xmlns:A.B.C="www.somesite.co.uk/A/B/C">
<Class>GetVersionRequest</Class>
<Errors>
<Error>
<Text>Some message</Text>
</Error>
</Errors>
</GetVersionResponse>

这是我的输入:
<Error>
<Namespace>xmlns:A.B.C</Namespace>
<NamespaceValue>www.somesite.com/A/B/C</NamespaceValue>
<Class>GetVersionRequest</Class>
<Message>Some message</Message>
</Error>

这是 Error.xsl 文件中的重要片段:
<xsl:template match="Error">                    
<xsl:variable name="ResponseType">
<xsl:value-of select="concat(substring-before(Class, 'Request'),'Response')"/>
</xsl:variable>
<xsl:variable name="NS">
<xsl:value-of select="Namespace"/>
</xsl:variable>
<xsl:variable name="NSV">
<xsl:value-of select="NamespaceValue"/>
</xsl:variable>

<xsl:element name="{$ResponseType}" namespace="{$NSV}" xml:space="default">
...
</xsl:template

它生成以下错误,因为我输入的 Namespace 元素中有一个冒号:

XSLT 2.0 调试错误:未知的命名空间前缀

如果我用下划线替换冒号,我会得到我需要的确切输出,但当然只有下划线。像这样:
<GetVersionResponse xmlns_A.B.C="www.somesite.co.uk/A/B/C">
<Class>GetVersionRequest</Class>
<Errors>
<Error>
<Text>Some message</Text>
</Error>
</Errors>
</GetVersionResponse>

我已经在网上搜索了几个小时,并认为我正在尝试将 xmlns: 作为文本插入,而不是使用正确的对象来生成 xmlns:MYTEXT 但不知道如何去做。

非常感谢您的帮助!!!!

最佳答案

这里的复杂因素是 xmlns:...就 XPath 数据模型而言, namespace 声明不是属性,因此您不能使用 xsl:attribute 创建它们。 .但是,在 XSLT 2.0 中,您可以使用 xsl:namespace 创建它们。 .

<xsl:template match="Error">
<xsl:element name="{substring-before(Class, 'Request')}Response">
<xsl:namespace name="{substring-after(Namespace, 'xmlns:')}"
select="NamespaceValue" />
<xsl:copy-of select="Class" />
<Errors>
<Error>
<Text><xsl:value-of select="Message" /></Text>
</Error>
</Errors>
</xsl:element>
</xsl:template>

Live demo
name xsl:namespace 的属性是您要绑定(bind)的前缀(因此不包括 xmlns: ),以及 select给出你想要绑定(bind)到的 URI。

关于xslt - xsl 生成带前缀的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26357926/

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