PDA

View Full Version : Importing Straight XHTML in XSLT


Arbitrator
01-07-2007, 12:20 AM
Basically, I want to pull the (XHTML) content of the content element and output it directly (as XHTML) in the <xht:div id="content"> element via the XSLT template. Unfortunately, I don’t know how to import the content of the content element in such a way. Would appreciate some help with this.

Below are the template and document, with some color‐coded code and comments to indicate the relevant areas.

Here’s the XSLT template:

<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:xht="http://www.w3.org/1999/xhtml">

<output method="html" media-type="application/xhtml+xml" omit-xml-declaration="no" version="1.0" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.1//EN" doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"/>

<template match="document">

<xht:html xml:lang="en-US">
<xht:head>

<xht:title>Just Some Guy’s Place: <value-of select="headers/title"/></xht:title>
<xht:meta name="Author" content="{headers/author}"/>

<xht:style type="text/css">
@import url("master.css") screen;
</xht:style>

</xht:head>
<xht:body>

<xht:div id="header">
<xht:h1>Just Some Guy’s Place</xht:h1>
</xht:div>
<xht:div id="navigation">
<xht:ul>
<xht:li>one</xht:li>
<xht:li>two</xht:li>
<xht:li>three</xht:li>
<xht:li>four</xht:li>
<xht:li>five</xht:li>
<xht:li>six</xht:li>
<xht:li>seven</xht:li>
<xht:li>eight</xht:li>
<xht:li>nine</xht:li>
<xht:li>ten</xht:li>
</xht:ul>
</xht:div>
<xht:div id="content">

<!-- Import raw XHTML from "content" element to here. -->
<!-- The raw XHTML should be output as identical XHTML. -->

</xht:div>
<xht:div id="footer">
And they lived happily ever after.
</xht:div>

</xht:body>
</xht:html>

</template>
</stylesheet>

Here’s the XML document:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="master.xsl"?>

<document xmlns:xht="http://www.w3.org/1999/xhtml" xml:lang="en-US">
<headers>

<title>Rōmaji, Hiragana, and Katakana Table</title>
<author>Patrick Garies</author>
<created>2007 January 6</created>
<updated>2007 January 6</updated>

<presentation type="text/css">

</presentation>

</headers>
<content>

<!-- I want to pull the XHTML content of this element. -->

<xht:table>
<xht:caption>
<xht:span>
<xht:span class="pre">
<xht:span xml:lang="ja">Rōmaji</xht:span>&#xa0;(<xht:span xml:lang="ja">ローマ字</xht:span>),
</xht:span>
<xht:span class="pre">
<xht:span xml:lang="ja">Hiragana</xht:span>&#xa0;(<xht:span xml:lang="ja">平仮名</xht:span>),
</xht:span> and
<xht:span class="pre">
<xht:span xml:lang="ja">Katakana</xht:span>&#xa0;(<xht:span xml:lang="ja">片仮名</xht:span>)
</xht:span> Table
</xht:span>
</xht:caption>
<xht:thead xml:lang="ja">
<xht:tr>
<xht:th id="rōmaji"><xht:abbr title="Rōmaji">R</xht:abbr></xht:th>

<!-- The remainder of the table is truncated here. -->

</content>
</document>

david_kw
01-07-2007, 01:39 AM
There is an xsl function xsl:copy-of which does a deep copy of the element. So I think the way to do it is to do a copy-of on each element in the <content> element.

Here is my take on it

master.xsl


<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:xht="http://www.w3.org/1999/xhtml">

<output method="html" media-type="application/xhtml+xml" omit-xml-declaration="no" version="1.0" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.1//EN" doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"/>

<template match="document">

<xht:html xml:lang="en-US">
<xht:head>

<xht:title>Just Some Guy&apos;s Place: <value-of select="headers/title"/></xht:title>
<xht:meta name="Author" content="{headers/author}"/>

<xht:style type="text/css">
@import url("master.css") screen;
</xht:style>

</xht:head>
<xht:body>

<xht:div id="header">
<xht:h1>Just Some Guys Place</xht:h1>
</xht:div>
<xht:div id="navigation">
<xht:ul>
<xht:li>one</xht:li>
<xht:li>two</xht:li>
<xht:li>three</xht:li>
<xht:li>four</xht:li>
<xht:li>five</xht:li>
<xht:li>six</xht:li>
<xht:li>seven</xht:li>
<xht:li>eight</xht:li>
<xht:li>nine</xht:li>
<xht:li>ten</xht:li>
</xht:ul>
</xht:div>
<xht:div id="content">

<!-- Import raw XHTML from "content" element to here. -->
<!-- The raw XHTML should be output as identical XHTML. -->

<call-template name="copycontent" />

</xht:div>
<xht:div id="footer">
And they lived happily ever after.
</xht:div>

</xht:body>
</xht:html>

</template>

<template name="copycontent">
<for-each select="/document/content/*">
<copy-of select="." />
</for-each>
</template>
</stylesheet>


master.xml


<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="master.xsl"?>

<document xmlns:xht="http://www.w3.org/1999/xhtml" xml:lang="en-US">
<headers>

<title>Romaji, Hiragana, and Katakana Table</title>
<author>Patrick Garies</author>
<created>2007 January 6</created>
<updated>2007 January 6</updated>

<presentation type="text/css">

</presentation>

</headers>
<content>

<!-- I want to pull the XHTML content of this element. -->

<xht:table>
<xht:caption>
<xht:span>
<xht:span class="pre">
<xht:span xml:lang="ja">Romaji</xht:span>&#xa0;(<xht:span xml:lang="ja">????</xht:span>),
</xht:span>
<xht:span class="pre">
<xht:span xml:lang="ja">Hiragana</xht:span>&#xa0;(<xht:span xml:lang="ja">???</xht:span>),
</xht:span> and
<xht:span class="pre">
<xht:span xml:lang="ja">Katakana</xht:span>&#xa0;(<xht:span xml:lang="ja">???</xht:span>)
</xht:span> Table
</xht:span>
</xht:caption>
</xht:table>
<!-- I closed the table to make valid xml. -->
</content>
</document>


See if that works or at least is close.

david_kw

david_kw
01-07-2007, 01:46 AM
If you want comments and text nodes too you'll have to add that to the for-each loop select like this

<for-each select="/document/content/*|/document/content/text()|/document/content/comment()">

david_kw

Arbitrator
01-07-2007, 01:55 AM
I stumbled upon copy-of just before you posted. Seems that <copy-of select="content/*"/> is all that I need for now.

I’ve never seen that call-template element before though. I think it may prove useful later if combined with some sort of if statement. Copying text nodes may also be useful. Not sure that there’s any utility in copying comments though since they’d only be visible in the generated source; well, there are conditional comments, I suppose, but my page isn’t currently compatible with Internet Explorer.

Anyway, thanks for the info. :p

david_kw
01-07-2007, 02:05 AM
Hmm good point. For some reason I had it in my mind that xsl:copy-of could only be from one node. Maybe that was xsl:copy I was thinking of.

david_kw