Yogesh Rustagi
Yogesh Rustagi

Reputation: 65

How to copy parent node namespace to child element using xslt?

My xml looks like which I created using Java JAXBContext and Marshaller. I want to format some part of xml only not the whole xml.

<?xml version="1.0" encoding="UTF-8"?>
<ns4:Requests xmlns:ns2="http://www.dummy.com/xsd/tublu/murmur_001" xmlns:ns3="http://www.dummy.com/xsd/CommonObjects_001" xmlns:ns4="http://www.dummy.com/xsd/naku_001">
   <ns4:RequestSetId>fhskgvseruigiu</ns4:RequestSetId>
   <ns4:RequestStream>CHAPP</ns4:RequestStream>
   <ns4:Request>
      <ns4:TrackAndTrace>
         <ns4:CPAId>003</ns4:CPAId>
         <ns4:CorrelationId>ytuty</ns4:CorrelationId>
      </ns4:TrackAndTrace>
   </ns4:Request>
   <ns4:Request>
      <ns4:TrackAndTrace>
         <ns4:CPAId>003</ns4:CPAId>
         <ns4:CorrelationId>cyuri7</ns4:CorrelationId>
      </ns4:TrackAndTrace>
   </ns4:Request>
</ns4:Requests>

I want to format like

<?xml version="1.0" encoding="UTF-8"?>
<ns4:Requests xmlns:ns2="http://www.dummy.com/xsd/tublu/murmur_001" xmlns:ns4="http://www.dummy.com/xsd/naku_001" xmlns:ns3="http://www.dummy.com/xsd/CommonObjects_001">
    <ns4:RequestSetId>fhskgvseruigiu</ns4:RequestSetId>
    <ns4:RequestStream>CHAPP</ns4:RequestStream>
    <ns4:Request xmlns:ns4="http://www.dummy.com/xsd/naku_001"><ns4:TrackAndTrace><ns4:CPAId>003</ns4:CPAId><ns4:CorrelationId>ytuty</ns4:CorrelationId></ns4:TrackAndTrace></ns4:Request>
    <ns4:Request xmlns:ns4="http://www.dummy.com/xsd/naku_001"><ns4:TrackAndTrace><ns4:CPAId>003</ns4:CPAId><ns4:CorrelationId>cyuri7</ns4:CorrelationId></ns4:TrackAndTrace></ns4:Request>
</ns4:Requests>

Upvotes: 0

Views: 335

Answers (1)

Farroque Ariff
Farroque Ariff

Reputation: 26

Here is the solution (by Transforming the XML Data using Java's XSLT APIs),

As you may also have noticed.. JAXB alone cannot meet this requirement, but after marshalling the object to a formatted XML String (as u have shown) you can then post process/transform it accordingly using a suitable XSLT file

So to get a linearized 'Request' element, just make use of the xsl shown below:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:strip-space elements="TrackAndTrace"/> 
    <xsl:strip-space elements="Request"/> 

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

</xsl:stylesheet>

Note: Also tested that above method/approach is working properly - used the Stylizer sample code (from https://docs.oracle.com/javase/tutorial/jaxp/xslt/transformingXML.html)

Cheers!

Update: If you want a solution that also preserves the original namespace prefix as shown in your question, follow this variation Add factory.setNamespaceAware(true); in the Stylizer code & Use this tweaked XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="w3.org/1999/XSL/Transform" xmlns:ns4="dummy.com/xsd/naku_001">
<xsl:strip-space elements="ns4:TrackAndTrace"/>
<xsl:strip-space elements="ns4:Request"/> 
   <xsl:template match="@*|node()">
       <xsl:copy>
           <xsl:apply-templates select="@*|node()"/>
       </xsl:copy>
   </xsl:template>
</xsl:stylesheet> 

Upvotes: 1

Related Questions