Ramesh
Ramesh

Reputation: 185

How to keep namespace prefix while performing XSLT processing

Please see the XSLT, input XML, desired output XML and the Actual output, i am getting below.

XSLT

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="grandParent">
        <xsl:copy>
            <xsl:apply-templates select="@*" />
            <childValues>
                <xsl:value-of select="normalize-space(.)" />
            </childValues>
            <xsl:apply-templates select="node()" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>             

Input XML--

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <netconf:rpc xmlns:netconf="urn:ietf:params:xml:ns:netconf:base:1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
message-id="81">
      <netconf:edit-config>
        <netconf:target>
          <netconf:url/>
        </netconf:target>
        <netconf:config>
          <requests module="E5100">
            <request action="create" userName="sigma" sessionId="_sessionId">
              <SSA>
                <NetworkName>NTWK-ntwk_nm</NetworkName>
                <PortNumber>2</PortNumber>
                <PortType>vdsl</PortType>
                <SSAProvision>
                  <UserDescr></UserDescr>
                  <SubscriberID></SubscriberID>
                </SSAProvision>
              </SSA>
            </request>            
              </requests>
            </netconf:config>
          </netconf:edit-config>
        </netconf:rpc>
      </soapenv:Body>
    </soapenv:Envelope>

Desired Output XML:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <netconf:rpc xmlns:netconf="urn:ietf:params:xml:ns:netconf:base:1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
message-id="81">
      <netconf:edit-config>
        <netconf:target>
          <netconf:url/>
        </netconf:target>
        <netconf:config>
          <requests module="E5100">
            <request action="create" sessionId="_sessionId" userName="sigma">
              <SSA>
                <NetworkName>NTWK-ntwk_nm</NetworkName>
                <!-- Port Number removed-->                 
                <PortType>vdsl</PortType>
                <SSAProvision>
                  <UserDescr/>
                  <SubscriberID/>
                </SSAProvision>
              </SSA>
            </request>            
              </requests>
            </netconf:config>
          </netconf:edit-config>
        </netconf:rpc>
      </soapenv:Body>
    </soapenv:Envelope>

Actual Output:

<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <Body>
    <rpc xmlns:netconf="urn:ietf:params:xml:ns:netconf:base:1.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    message-id="81">
      <edit-config>
        <target>
          <url/>
        </target>
        <config>
          <requests module="E5100">
            <request action="create" sessionId="_sessionId" userName="sigma">
              <SSA>
                <NetworkName>NTWK-ntwk_nm</NetworkName>
                <!-- want to remove this PortNumber completely-->
                <PortNumber/>                
                <PortType>vdsl</PortType>
                <SSAProvision>
                  <UserDescr/>
                  <SubscriberID/>
                </SSAProvision>
              </SSA>
            </request>            
              </requests>
            </config>
          </edit-config>
        </rpc>
      </Body>
    </Envelope>

Upvotes: 1

Views: 814

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

To remove elements that have no content, you can add a template rule

<xsl:template match="*[not(child::node())]"/>

As others have remarked, the loss of namespace information is a bug in the toolchain being used. I would suggest (a) establishing exactly what toolchain is being used, (b) checking whether later versions of the same tools exist in which the bug is possibly fixed, (c) failing that, moving to a different XSLT processor and/or XML parser.

Sometimes the easiest way to discover what XSLT processor is being used is to add something like:

<xsl:template match="/">
  <xsl:comment>Generated using <xsl:value-of select="system-property('xsl:vendor')"/></xsl:comment>
  <xsl:apply-templates/>
</xsl:template>

Upvotes: 2

Related Questions