1

With a browser, I want to transform XML which may contain some HTML, with an XSL stylesheet. In this article, user Mads Hansen wrote:

If your HTML is well-formed, then just embed the HTML tags without escaping or wrapping in CDTATA. If at all possible, it helps to keep your content in XML. It gives you more flexibility for transforming and manipulating the document. You could set a namespace for the HTML, so that you could disambiguate your HTML tags from the other XML wrapping it.

I like the proposed solution, but can't make it work. I used h as the namespace for html:

temp.xml

<?xml version='1.0' encoding='UTF-8' ?>
<?xml-stylesheet type='text/xsl' href='temp.xsl'?>
<root xmlns:h="http://www.w3.org/1999/xhtml">
  <MYTAG title="thisnthat">
    text before ol
    <h:ol>
      <h:li>item</h:li>
      <h:li>item</h:li>
    </h:ol>
    text after ol
  </MYTAG>
</root>

temp.xsl

<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:h="http://www.w3.org/1999/xhtml">
  <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template match="/root">
    <html lang="en-US">
      <head>
        <meta charset="UTF-8" />
        <title></title>
      </head>
      <body>
        <xsl:apply-templates />
      </body>
    </html>
  </xsl:template>
  <xsl:template match="MYTAG">
    <h3>
      <xsl:value-of select="@title" />
    </h3>
    <xsl:apply-templates />
  </xsl:template>
</xsl:stylesheet>

The output (from Firefox 18) is:

thisnthat
text before ol item item text after ol 
Community
  • 1
  • 1
flymike
  • 883
  • 2
  • 8
  • 13
  • Is the output what is rendered in the browser window or the actual HTML as transformed? If it's the former, please show the HTML (use Firebug). Also, without an identity-transform the last `apply-templates` will just write out the text of the `ol` (i.e. not copy the nodes). – Jim Garrison Jan 11 '13 at 00:44
  • 1
    "I like the proposed solution, but can't make it work." Can't make what work? What's the expected outcome? And please show us the raw output, not just how it looks in Firefox. – JLRishe Jan 11 '13 at 01:57
  • So, What is the question? I don't see one. – Dimitre Novatchev Jan 11 '13 at 04:03

1 Answers1

0

Since you are generating the final HTML and are in control, I'm not sure why you want to use namespaces here. You'd need to do that only if there was a conflict between your custom tags and standard HTML -- i.e. if you had a custom <a...> tag that had different semantics from HTML. I got your transform to work by

a) Removing all HTML namespacing

b) Adding an identity transform

test.xml

<?xml version='1.0' encoding='UTF-8' ?>
<?xml-stylesheet type='text/xsl' href='test.xsl'?>
<root>
    <MYTAG title="thisnthat">
        text before ol
        <ol>
            <li>item</li>
            <li>item</li>
        </ol>
        text after ol
    </MYTAG>
</root>

test.xsl

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="UTF-8" indent="yes"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/root">
        <html lang="en-US">
            <head>
                <meta charset="UTF-8" />
                <title></title>
            </head>
            <body>
                <xsl:apply-templates />
            </body>
        </html>
    </xsl:template>
    <xsl:template match="MYTAG">
        <h3>
            <xsl:value-of select="@title" />
        </h3>
        <xsl:apply-templates />
    </xsl:template>
</xsl:stylesheet>
Jim Garrison
  • 81,234
  • 19
  • 144
  • 183