Search code examples
xsltsaxoncdata

XSLT Tranformation of XML file containing CDATA


I have to transform an xml file using XSLT.

I had to get in the output the some structure of the input file file, expected some changes in some elements exsiting on a CDATA element.

<soapenv:Envelope xmlns:aa="http://example.com"
                  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <aa:importOrder>
         <aa:orderNumber>00501010000342</aa:orderNumber>
         <aa:data>
            <![CDATA[
                <?xml version="1.0" encoding="UTF-8"?>
                    <OrderImport xmlns="http://test.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema">
                        <OrderNumber>00501010000342</OrderNumber>
                        <Application>
                            <Student>
                                    <DataElement>
                                            <Name>age</Name>
                                            <Type>Int</Type>
                                            <Value>13</Value>
                                    </DataElement>
                                    <DataElement>
                                            <Name>firstName</Name>
                                            <Type>String</Type>
                                            <Value>taha</Value>
                                    </DataElement>
                            </Student>
                        </Application>
                    </OrderImport>
                ]]>
            </aa:data>
      </aa:importOrder>
   </soapenv:Body>
</soapenv:Envelope> 

My expected output should be like this:

<soapenv:Envelope xmlns:aa="http://example.com"
                  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <aa:importOrder>
         <aa:orderNumber>00501010000342</aa:orderNumber>
         <aa:data>
            <![CDATA[
                <?xml version="1.0" encoding="UTF-8"?>
                    <OrderImport xmlns="http://test.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema">
                        <OrderNumber>00501010000342</OrderNumber>
                        <Application>
                            <Student>
                                    <DataElement>
                                            <Name>age</Name>
                                            <Type>Int</Type>
                                            **<Value>Other Value</Value>**
                                    </DataElement>
                                    <DataElement>
                                            <Name>firstName</Name>
                                            <Type>String</Type>
                                            **<Value>Other Value</Value>**
                                    </DataElement>
                            </Student>
                        </Application>
                    </OrderImport>
                ]]>
            </aa:data>
      </aa:importOrder>
   </soapenv:Body>
</soapenv:Envelope>

For that i used this xsl file:

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

<xsl:template match="DataElement">
    <p>
        <xsl:apply-template select="name"/>
    </p>
</xsl:template>

<xsl:template match="Name | Type">
    <xsl:value-of select="."/>
</xsl:template>

<xsl:template match="Value">
    Other Value
</xsl:template>

but I didn't had the expected output.

Thanks in Advance


Solution

  • To parse the inner XML in the CDATA you can use parse-xml, then push the nodes through templates that change the element value, then reserialize and make sure to define the aa:data as a CDATA section element:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:aa="http://example.com"
        exclude-result-prefixes="#all"
        version="3.0">
    
      <xsl:mode on-no-match="shallow-copy"/>
    
      <xsl:output method="xml" cdata-section-elements="aa:data"/>
    
      <xsl:template match="aa:data">
          <xsl:copy>
              <xsl:variable name="transformed">
                  <xsl:apply-templates select="parse-xml(replace(., '^\s+', ''))/node()"/>
              </xsl:variable>
              <xsl:value-of select="serialize($transformed, map { 'method' : 'xml', 'omit-xml-declaration' : false() })"/>
          </xsl:copy>
      </xsl:template>
      
      <xsl:template match="Value" xpath-default-namespace="http://test.com">
          <xsl:copy>Other Value</xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>