1

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

  • I need to REMOVE all the tags if it has no value or whitespace(s). eg: remove <abc></abc>
  • Using the below XSLT, the empty tags are converted to self closing tags (which i totally want to remove), Also the namespaces are getting removed eg: <soapenv:Body> gets transformed to <Body>(which i want to keep as it is)
  • Please help me to achieve the desired output without removing namespace prefix.

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>
8
  • 1. Your problem cannot be reproduced using your code - see: xsltransform.net/94rmq7j -- 2. There is no grandParent element in your XML input, so the second template does not do anything. Commented Jul 18, 2016 at 13:48
  • 1
    Thanks michael, i am very much new to this and due to some reason i have to make it work asap. I agree that on online xslt parsers this works but in the environment i am trying and in some company specific tool which is based on Java, it is removing the namespace prefixes. Any pointer on what could go wrong or what should i need to check. Commented Jul 18, 2016 at 14:07
  • 1
    I assume you are aware that the nodes of your output document are still in the correct namespace (the namespace is not removed) and any parser or XML processing tool should work, no matter whether you set a default namespace or set the namespace explicitly via a prefix. Commented Jul 18, 2016 at 14:10
  • 1
    @Ramesh Sorry, I have no idea. All I can say that a conforming XSLT processor would not produce the result you claim. Apparently your question is not about XSLT. Commented Jul 18, 2016 at 14:24
  • 1
    @Ramesh: Congrats on giving a very thorough documentation of the problem -- input, code, expected output, and actual output. What XSLT environment are you using, that seems to be producing nonconformant output? It sounds to me like your company-specific java tool may be at fault. Commented Jul 18, 2016 at 15:12

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.