0

Hi I am unable to traverse my input XML file to get desired output. Kindly help. Desired output:

<DocFWImport><Header senderID="ABC1234"/></Request></DocFWImport>

My XSLT File:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rout="http://safe.com/RoutePlanner/" xmlns:dp="http://www.datapower.com/extensions" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" extension-element-prefixes="dp" exclude-result-prefixes="dp">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <xsl:apply-templates select="/Envelope/Body/ABCCustomMsg/*"/>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Input XML:

<Envelope 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rout="http://safe.com/RoutePlanner/">

<Body> 
<rout:ABCCustomMsg 
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
>

<DocFWImport><Header senderID="ABC1234"/></Request></DocFWImport></ns1:ABCCustomMsg>
</Body>
</Envelope>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
phani
  • 1

2 Answers2

0

You need to use namespace prefixes in your XPaths. That's what they're for:

<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:rout="http://safe.com/RoutePlanner/" 
                xmlns:dp="http://www.datapower.com/extensions" 
                xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                extension-element-prefixes="dp" exclude-result-prefixes="dp">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <xsl:apply-templates select="/soapenv:Envelope/soapenv:Body/rout:ABCCustomMsg/*"/>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

This should produce the output:

<DocFWImport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rout="http://safe.com/RoutePlanner/"><Header senderID="ABC1234"/></Request></DocFWImport>

If you actually want to remove the namespaces from the elements you are copying (as your example output has no namespaces), you can do so like this:

<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:rout="http://safe.com/RoutePlanner/" 
                xmlns:dp="http://www.datapower.com/extensions" 
                xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                extension-element-prefixes="dp" exclude-result-prefixes="dp">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <xsl:apply-templates select="/soapenv:Envelope/soapenv:Body/rout:ABCCustomMsg/*"/>
    </xsl:template>

    <xsl:template match="@*|text()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

This should produce the desired output that you have shown.

JLRishe
  • 99,490
  • 19
  • 131
  • 169
0

Your elements are in a namespace, but you are selecting elements in no namespace. See for example XSLT with XML source that has a default namespace set to xmlns

Community
  • 1
  • 1
Michael Kay
  • 156,231
  • 11
  • 92
  • 164