XSLT - 在一个节点中添加两个名称空间 [英] XSLT - Add two namespaces in one node
问题描述
这是我使用 XSLT
的 XML
:
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<RequestResponse xmlns="http://tempuri.org/">
<RequestResult>
<Message>Request inserted successfully.</Message>
<Response>true</Response>
</RequestResult>
</RequestResponse>
</s:Body>
</s:Envelope>
我想在 RequestResult
节点中添加两个命名空间(我想要的结果):
And I want to add two namespaces in the RequestResult
node (The result I want):
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<RequestResponse xmlns="http://tempuri.org/">
<RequestResult xmlns:a="http://schemas.datacontract.org/2004/07/MockupTesting" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Message>Request inserted successfully.</a:Message>
<a:Response>true</a:Response>
</RequestResult>
</RequestResponse>
</s:Body>
</s:Envelope>
我使用这个 XSLT:
I use this XSLT:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<xsl:apply-templates />
</s:Body>
</s:Envelope>
</xsl:template>
<xsl:template match="RequestResult |RequestResult //*">
<xsl:element name="a:{name()}"
namespace="http://schemas.datacontract.org/2004/07/MockupTesting">
<xsl:copy-of select="namespace::*" />
<xsl:apply-templates select="node()|@*" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
但我需要添加到 SolicitarPeticionResult
节点(已经包含命名空间):
But I need to add to the SolicitarPeticionResult
node (which already contains a namespace):
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
推荐答案
当 (i) X 的名称不是静态已知的,并且 (ii) 命名空间实际上没有在元素 X 中使用时,向元素 X 添加命名空间X 的名称或其任何属性的名称:
To add a namespace to an element X when (i) the name of X is not known statically, and (ii) the namespace is not actually used in the name of X or in the name of any of its attributes:
(a) 在 XSLT 2.0 中,使用 xsl:namespace
指令
(a) In XSLT 2.0, use the xsl:namespace
instruction
(b) 在 XSLT 1.0 中,使用 xsl:copy-of
从已经具有此命名空间的元素复制命名空间.比如创建一个单独的文件dummy.xml
(b) In XSLT 1.0, use xsl:copy-of
to copy the namespace from an element that already has this namespace. For example, create a separate document dummy.xml
<i:dummy xmlns:i="http://www.w3.org/2001/XMLSchema-instance"/>
然后做
<xsl:copy-of select="document('dummy.xml')/*/namespace::i"/>
这篇关于XSLT - 在一个节点中添加两个名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!