I saw the post XSLT : Add a namespace declaration to the root element
The @StuartLC answer works in the root. I need help... ¿How would you add a new namespace to a non-root node?
The input XML document (yes, a horror but it's from the client)
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<S:Body xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<ns3:Response xmlns="ttn/xml" xmlns:ns2="rtm/xml" xmlns:ns3="ws/xml">
<ns3:record>
<registryID>
<registryNumber>232019324</registryNumber>
</registryID>
<ns2:Date>28-08-2019 09:12:32</ns2:Date>
<ns2:registry>
<ns2:type>otp</ns2:type>
<sender>
<ID>260</ID>
</sender>
</ns2:registry>
</ns3:record>
</ns3:Response>
</S:Body>
</soapenv:Envelope>
And I'd want to obtain:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<S:Body xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<ns3:Response xmlns:ns1="ttn/xml" xmlns="ttn/xml" xmlns:ns2="rtm/xml" xmlns:ns3="ws/xml">
<record>
<ns1:registryID>
<ns1:registryNumber>232019324</ns1:registryNumber>
</ns1:registryID>
<ns2:Date>28-08-2019 09:12:32</ns2:Date>
<ns2:registry>
<ns2:type>otp</ns2:type>
<ns1:sender>
<ns1:ID>260</ns1:ID>
</ns1:sender>
</ns2:registry>
</record>
</ns3:Response>
</S:Body>
</soapenv:Envelope>
I need to add/remove some prefixes nodes. Eg node <ns3:record>
-> <record>
or node <registryID>
-> <ns1:registryID>
or node <sender>
-> <ns1:sender>
.... there are more nodes xml imput but I've only put a few.
I need include ns1 in the node Response because when i import to SAP PO it says "ns1 is not declared". I supposed it i added the prefix ns1 to the node but i don't know. I tried manually in test SAP include ns1 in the node and work it. After in other transformations xslt, only i need the node Response of all xml to map.
This is xslt that im using:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="ttn/xml" xmlns:ns2="rtm/xml" xmlns:ns3="ws/xml" xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<xsl:output omit-xml-declaration="yes" method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name ="mynoderecord">record</xsl:variable>
<xsl:variable name ="mynodeResponse">Response</xsl:variable>
<xsl:variable name ="mynoderegistryNumber">registryNumber</xsl:variable>
<xsl:variable name ="mynodeRegistryID">registryID</xsl:variable>
<xsl:variable name ="mynodesender">sender</xsl:variable>
<xsl:template match="*" >
<xsl:choose>
<xsl:when test="local-name() = $mynodeResponse">
<xsl:element name="ns3:{local-name()}" xmlns:ns3="ws/xml">
<xsl:attribute name="ns1:nsdf" namespace="ttn/xml">sdf</xsl:attribute>
<xsl:for-each select="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:element>
</xsl:when>
<xsl:when test="local-name() = $mynoderegistryNumber" >
<xsl:element name="ns1:{name()}" xmlns:ns1="ttn/xml">
<!-- procesar atributos de nodo -->
<xsl:for-each select="@*">
<!-- eliminar prefijo de atributo -->
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:element>
</xsl:when>
<xsl:when test="local-name() = $mynodesender" >
<xsl:element name="ns1:{name()}" xmlns:ns1="ttn/xml">
<!-- procesar atributos de nodo -->
<xsl:for-each select="@*">
<!-- eliminar prefijo de atributo -->
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:element>
</xsl:when>
<xsl:when test="local-name() = $mynodeRegistryID" >
<xsl:element name="ns1:{name()}" xmlns:ns1="ttn/xml">
<!-- procesar atributos de nodo -->
<xsl:for-each select="@*">
<!-- eliminar prefijo de atributo -->
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:element>
</xsl:when>
<xsl:when test="local-name() = $mynoderecord">
<xsl:element name="{local-name()}" xmlns:ns3="ws/xml">
<!-- procesar atributos de nodo -->
<xsl:for-each select="@*">
<!-- eliminar prefijo de atributo -->
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{name()}">
<!-- procesar atributos de nodo -->
<xsl:for-each select="@*">
<!-- eliminar prefijo de atributo -->
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Searching i saw that adding an attribute to node <ns3:Response>
, namespace ns1 is automatically added... but i don't like.
Surely there is a simply solution with better code ...
the xsl will drive me crazy.
I tried, but I couldn't find any easier suitable solution .
Could you please advise me?
Thanks