6

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.

6
  • 2
    Which XSLT 1.0 processor do you use? You need an extension function that can parse a string with XML into nodes. Commented Nov 27, 2013 at 20:31
  • @MartinHonnen Which function do you have in mind? Commented Nov 29, 2013 at 5:29
  • @user3016153, depending on the XSLT processor there are existing solutions or it is easy to implement one, for instance with Microsoft's XslCompiledTransform you can use an extension function or object that them makes use of an XPathDocument to parse the XML. With Microsoft's MSXML you can use an extension function implemented in JScript that simply needs to create e.g. function parseXml(string) { var doc = new ActiveXObject('Msxml2.DOMDocument.3.0'); doc.loadXML(string); return doc; }. Commented Nov 29, 2013 at 10:24
  • I tried but getting java.lang.NoSuchMethodException: For extension function, could not find method org.apache.xml.utils.NodeVector.parse([ExpressionContext,] Commented Nov 30, 2013 at 8:42
  • 1
    In my commented I made suggestions on how to implement an extension function for Microsoft's XslCompiledTransform XSLT processor and for Microsoft's MSXML XSLT processor. As all that is processor dependent it will not work with other processors. I asked you in the first comment to tell us which XSLT processor you use. If you use Java then I would suggest to use a version of Saxon having an extension function to parse XML into nodes, like saxonica.com/documentation/index.html#!functions/saxon/parse. Commented Nov 30, 2013 at 14:06

1 Answer 1

1

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

2 Comments

The code is working fine.and I am getting expected result. Thanks a lot .. Is there any alternative way, as I dont want to use substring function.
I believe the substring function provides the shortest path to your expected output. Why wouldn't you want to use it?

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.