0

I have to transform my input xml using XSLT. It contains, CDATA and I need to extract elements from CDATA and then I have to rename the tag.

Below is my input xml :

 <getArtifactContentResponse>
        <return>
             <![CDATA[
       <metadata>
        <overview>        
            <name>scannapp</name>
            <developerId>developer702</developerId>
            <stateId>2</stateId>
            <serverURL>dddd</serverURL>
            <id>cspapp1103</id>
            <description>scann doc</description>
            <hostingTypeId>1</hostingTypeId>
     </overview>
    </metadata>
      ]]>
      </return>
    </getArtifactContentResponse> 

And the expected output is :

 <?xml version="1.0" encoding="UTF-8"?>
   <metadata >
    <information>        
        <name>scannapp</name>
        <developerId>developer702</developerId>
        <stateId>2</stateId>
        <serverURL>ddddd</serverURL>
        <id>cspapp1103</id>
        <description>scann doc</description>
        <hostingTypeId>1</hostingTypeId>        
    </Information>
</metadata>

XSLT I am using is below :

<xsl:output method="xml" version="1.0" encoding="UTF-8" />
<xsl:template match="/">
    <xsl:value-of select="//ns:getArtifactContentResponse/ns:return/text()" disable-output-escaping="yes"/>
</xsl:template>


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

With this I am able to exrtact the CDATA but it is not renaming the element 'overview' to 'Information' .

Transformed xml is below :

<?xml version="1.0" encoding="UTF-8"?>

   <metadata>
    <overview>        
        <name>scannapp</name>
        <developerId>developer702</developerId>
        <stateId>2</stateId>
        <serverURL>dddddd</serverURL>
        <id>cspapp1103</id>
        <description>scann doc</description>
        <hostingTypeId>1</hostingTypeId>        
    </overview>
</metadata>

Can someone tell me how I can rename the tag after extracting the CDATA? I don't understand what I am missing here?

Thanks in Advance

krishh
  • 1
  • 4
  • 2
    If you only have XSLT version 1.0 available, you'd need to run another XSLT on the output of your initial XSLT to produce the desired output. Content of CData is considered plain text so it can't be processed like normal XML fragment by XSLT. – har07 Apr 28 '16 at 02:59
  • 1
    Related: 1. [Convert an xml element whose content is inside CDATA](http://stackoverflow.com/questions/2067116/convert-an-xml-element-whose-content-is-inside-cdata), 2. [Transform XML from CDATA using XSL](http://stackoverflow.com/questions/18612639/transform-xml-from-cdata-using-xsl) – har07 Apr 28 '16 at 03:00
  • Not clear to me. Can you please explain with above example? – krishh Apr 28 '16 at 04:06
  • Link no.1 showcases the *2 transforms approach*. You already have XSLT for the first transform step. Now create another XSLT to transform output of the first transform into the final expected output... – har07 Apr 28 '16 at 04:21
  • Now, I have two xslt. But can I apply these two xslt sequentially? I should not use two xsl files explicilty. – krishh Apr 28 '16 at 04:32
  • If you don't want to do a two separate transformation, then the remaining options are to use higher XSLT version (link 2), or do simple string replacement i.e *replace text 'overview' with 'information'*, which is a rather hacky and error prone approach.. – har07 Apr 28 '16 at 04:41

1 Answers1

0

There are no elements in your CDATA, there is only text. That's what CDATA means: "this stuff might look like markup, but I want it treated as text".

Turning text into elements is called parsing, so to extract the elements from the text in your CDATA you are going to have to parse it. There's no direct way to do this in XSLT until you get to XSLT 3.0 (which has a parse-xml() function). Some XSLT processors have an extension function to do it; in some (I believe) the exslt:node-set() function does this if you supply a string as input. With others, you can call out to your own Java or Javascript code to do the parsing. So it all becomes processor-dependent.

Another approach is to output the XML in your CDATA section using the disable-output-escaping trick, and then process it in a second transformation.

The best approach is to get rid of the CDATA tags before you start. They should never have been put there in the first place.

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