问题
I am very new in XSLT. I need to crate a XML like this.
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetRecordResponse xmlns="urn:RedIron.RetailRepository.Services.SearchService">
<GetRecordResult xmlns:a="urn:RedIron.RetailRepository.Core" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
I am able to produce the xml upto this lavel. but as there is multi lavel namespace is used I got confused.
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetRecordResponse xmlns="urn:RedIron.RetailRepository.Services.SearchService"/>
</s:Body>
</s:Envelope>
I am using this XSLT for the same
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match = "/" >
<s:Envelope >
<s:Body>
<xsl:element name = "GetRecordResponse" namespace = "urn:RedIron.RetailRepository.Services.SearchService" >
</xsl:element>
</s:Body>
</s:Envelope>
</xsl:template>
</xsl:stylesheet>
回答1:
If you want to add these namespace nodes, you can use xsl:namespace
. This is only available in processors supporting XSLT2.0 though.
<xsl:element name="GetRecordResponse" namespace="urn:RedIron.RetailRepository.Services.SearchService">
<xsl:element name="GetRecordResult" namespace="urn:RedIron.RetailRepository.Services.SearchService">
<xsl:namespace name="a" select="'urn:RedIron.RetailRepository.Core'" />
<xsl:namespace name="i" select="'http://www.w3.org/2001/XMLSchema-instance'" />
</xsl:element>
</xsl:element>
Alternatively, you can probably just write out the element names and there namespace declarations literally
<GetRecordResponse xmlns="urn:RedIron.RetailRepository.Services.SearchService" >
<GetRecordResult xmlns:a="urn:RedIron.RetailRepository.Core" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
</GetRecordResponse>
Note that if you move the creation of GetRecordResult
to a different template, for example, you might need to specify the default namespace on it, as it will no longer inherit the default namespce from GetRecordResponse
<GetRecordResult xmlns="urn:RedIron.RetailRepository.Services.SearchService"
xmlns:a="urn:RedIron.RetailRepository.Core"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
来源:https://stackoverflow.com/questions/41483583/multi-level-and-multiple-namespace-in-xml-using-xslt