Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse the XML Elements inside the CData Using XSLT and Print to HTML

Tags:

xslt

Input xml is

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

Below is the stylesheet which I have developed. I am able to retrieve the XML inside Cdata but not able to fetch the elements value.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
<xsl:output method="html" encoding="utf-8" omit-xml-declaration="no" indent="no"/>
   <xsl:template match="/">
      <html>
         <body>
            <h1>Company Details</h1>
            <table border="1">
               <tr>
                  <th>name</th>
                  <th>developerId</th>
                  <th>Id</th>
               </tr>table

               <xsl:variable name ="data" select="//getArtifactContentResponse/return/node()" />
                  <tr>
                     <td>
                        <xsl:value-of select="$data/metadata/overview/name" disable-output-escaping="yes"/>


                     </td>
                     <td>
                        <xsl:value-of select="$data/metadata/overview/developerId" />
                     </td>
                     <td>
                        <xsl:value-of select="$data/metadata/overview/Id" />
                     </td>
                  </tr>

            </table>

         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

Output coming as

<html><body><h1>Company Details</h1><table border="1"><tr><th>name</th><th>developerId</th><th>serverURL</th></tr>table


                  <tr><td></td><td></td><td></td></tr></table></body></html>

Expected output

<html><body><h1>Company Details</h1><table border="1"><tr><th>name</th><th>developerId</th><th>serverURL</th></tr>table


                  <tr><td>scannapp</td><td>developer702</td><td>cspapp1103</td></tr></table></body></html>

I want to take the value name,developerId,Id and print to HtML. How to do that Please help me. Using XSLT version1.0.

like image 612
user3042857 Avatar asked Nov 27 '13 17:11

user3042857


1 Answers

AFAIK, there's no way to parse CDATA as XML.

Using an extension function to parse the section as text would be nice, but not really necessary. Here's an example that extracts the three items you're after:

...
<xsl:variable name ="cdata" select="/getArtifactContentResponse/return" />

<name>
    <xsl:value-of select="substring-before(substring-after($cdata, '&lt;name&gt;'), '&lt;/name&gt;')"/>
</name>

<developerId>
    <xsl:value-of select="substring-before(substring-after($cdata, '&lt;developerId&gt;'), '&lt;/developerId&gt;')"/>
</developerId>

<id>
    <xsl:value-of select="substring-before(substring-after($cdata, '&lt;id&gt;'), '&lt;/id&gt;')"/>
</id>
...
like image 183
michael.hor257k Avatar answered Oct 16 '22 03:10

michael.hor257k