1

I wonder if anybody could help me please with a tricky problem I am having. Probably asking the impossible here but would be interested to hear any suggestions.

I have an XML file that in one of the nodes contains the HTML of a page. I would like to take the data in that node and some how create an HTML file from it. So, from the example XML below, I want to transform and pick cell[2] node from //dvm/rows/row and place that in another file so that I can actually see what that HTML mark-up shows and display as a proper web page rather than the code.

If I put it in and XSL like this, would it work?

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="html"/>
<xsl:template match="/">
    <xsl:value-of select="//dvm/rows/row/cell[2]" disable-output-escaping="yes"/>   
</xsl:template> 
</xsl:stylesheet>

The XML is:

<dvm>
  <description>This is a DVM file</description>
  <columns>
    <column name="lang"/>
    <column name="text"/>
    <column name="code" qualifier="true" order="1"/>
    <column name="type" qualifier="true" order="2"/>
    <column name="subj"/>
    <column name="urem"/>
    <column name="vreq"/>
 </columns>
 <rows>
   <row>
     <cell>English(UK)</cell>
     <cell><![CDATA[<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"       
           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
           <html xmlns="http://www.w3.org/1999/xhtml">
           <head>
              <title>My HTML File</title>
           </head>
           <body>
           <div class="text">
             <p>This is a line of text</p>
             <p>This is a line of text</p>
             <p>This is a line of text</p>  
           </div>
           </body>
           </html>]]>
     </cell>
     <cell>Code 1</cell>
     <cell>Text</cell>
     <cell>HTML Block 1</cell>
     <cell/>
     <cell/>
   </row>
 </rows>
</dvm>

2 Answers 2

2

You must get rid of the CDATA section and use the text it contains as regular markup. The DOCTYPE should also be removed (it will be generated by the transformation):

<?xml-stylesheet type="text/xsl" href="delete_3.xsl" ?>
<dvm>
    <description>This is a DVM file</description>
    <columns>
        <column name="lang"/>
        <column name="text"/>
        <column name="code" qualifier="true" order="1"/>
        <column name="type" qualifier="true" order="2"/>
        <column name="subj"/>
        <column name="urem"/>
        <column name="vreq"/>
    </columns>
    <rows>
        <row>
            <cell>English(UK)</cell>
            <cell>
               <html xmlns="http://www.w3.org/1999/xhtml">
               <head>
                  <title>My HTML File</title>
               </head>
               <body>
               <div class="text">
                 <p>This is a line of text</p>
                 <p>This is a line of text</p>
                 <p>This is a line of text</p>
               </div>
               </body>
               </html>
      </cell>
            <cell>Code 1</cell>
            <cell>Text</cell>
            <cell>HTML Block 1</cell>
            <cell/>
            <cell/>
        </row>
    </rows>
</dvm>

Then the transformation that produces the XHTML contained in the second cell element is simple:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
 doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <xsl:copy-of select="/*/rows/*/cell[2]/node()"/>
 </xsl:template>
</xsl:stylesheet>

When now I open the file: c:\temp\delete\delete2.xml with IE, the result is:

<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <title>My HTML File</title>
   </head>
   <body>
      <div class="text">
         <p>This is a line of text</p>
         <p>This is a line of text</p>
         <p>This is a line of text</p>
      </div>
   </body>
</html>

and this is displayed by the browser as:

My HTML File

This is a line of text

This is a line of text

This is a line of text

Sign up to request clarification or add additional context in comments.

Comments

0
<xsl:value-of select="cell[2]" disable-output-escaping="yes" />

Note that since your <cell> contains XHTML, there is no real need for a CDATA section. XHTML and XML are compatible, you can transport the one inside the other without converting it to a string first.

6 Comments

Thanks! I've made a slight modification to my question as I tried putting in your suggestion to my XSL template but it just showed the code block in text form (as I was doing a 'view source'). Am I calling it the right way?
So you did see HTML code when you looked at the source? I'm confused. This is the expected result, after all. No?
Sorry - I worded the last comment a bit badly. When I try an view the XSL transformation in a browser, it was showing the code block in text form (I was meant to say as if you were doing a 'view source' not actually doing one). What I was intending to do was some how see the rendered result of the HTML that I picked out i.e. the web page of that code segment. Hope that made more sense!
@JaXL Browser-integrated XSLT does not support disable-output-escaping. You will have to get rid of the CDATA section.
That makes sense :) However, I have new issue with it. The first "<" in <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> is causing parsing errors along with things like &copy; if I want to use a copyright symbol as part of the HTML code block. Another thing, to eliminate this problem, would by adding some scripting to dynamically remove the CDATA tag suffice in rendering the code?
|

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.