I would like to transform this XML:
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ens:query xmlns:ens="urn:example.com">
<ens:queryString>Select Example</ens:queryString>
<ens:param>
<ens:name>maxRows</ens:name>
<ens:value>123</ens:value>
</ens:param>
<ens:param>
<ens:name>startingRow</ens:name>
<ens:value>456</ens:value>
</ens:param>
</ens:query>
</soapenv:Body>
</soapenv:Envelope>
into this:
<queryParameters>
<maxRows>123</maxRows>
<startingRow>456</startingRow>
</queryParameters>
I know how to copy nodes, but the namespace prefix "ens" and the fact that ens:param has 2 entries confuses the hell out of me. I tried to adress the single nodes though 1 and 2, but to no avail. I simply have no idea how to reference and distinguish them properly at the same time. Also I need to create node names from node values. Could someone help me about his issue?
CodePudding user response:
Seems quite simple, not sure why you're having such problems with it:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ens="urn:example.com"
exclude-result-prefixes="soapenv ens">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/soapenv:Envelope">
<queryParameters>
<xsl:for-each select="soapenv:Body/ens:query/ens:param">
<xsl:element name="{ens:name}">
<xsl:value-of select="ens:value"/>
</xsl:element>
</xsl:for-each>
</queryParameters>
</xsl:template>
</xsl:stylesheet>
But do note that this will fail if the value of ens:name
is not a valid XML element name.