问题
This is my XML
using XSLT
:
<?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>
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>
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>
But I need to add to the SolicitarPeticionResult
node (which already contains a namespace):
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
回答1:
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) In XSLT 2.0, use the xsl:namespace
instruction
(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"/>
and then do
<xsl:copy-of select="document('dummy.xml')/*/namespace::i"/>
回答2:
Why don't you do simply:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="http://tempuri.org/">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="RequestResult">
<RequestResult xmlns="http://tempuri.org/" xmlns:a="http://schemas.datacontract.org/2004/07/MockupTesting" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<xsl:apply-templates/>
</RequestResult>
</xsl:template>
<xsl:template match="RequestResult/*">
<xsl:element name="a:{name()}" namespace="http://schemas.datacontract.org/2004/07/MockupTesting">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
来源:https://stackoverflow.com/questions/40586119/xslt-add-two-namespaces-in-one-node