simple identity transform - what am I missing?

Here should go questions about transforming XML with XSLT and FOP.
stevedonie
Posts: 2
Joined: Fri Feb 13, 2009 7:27 am

simple identity transform - what am I missing?

Post by stevedonie »

I must be missing something simple. Using this input:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!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" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Simple</title>
<link rel="stylesheet" type="text/css" media="screen,print" href="style.css" />
</head>
<body>
<div style="text-align: center; ">
<p>
<a href="Next.html">
Next >
</a>
</p>
</div>
</body>
</html>
and this stylesheet:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:preserve-space elements="*"/>

<xsl:output omit-xml-declaration="no" encoding="UTF-8"
doctype-public="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="div">
<description>n/a</description>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>
I am not seeing what I expect, which is that the div gets a description added before it. Am I missing something? Seems like the second template never matches.
stevedonie
Posts: 2
Joined: Fri Feb 13, 2009 7:27 am

Re: simple identity transform - what am I missing?

Post by stevedonie »

I think I found it! I was transforming XHTML, and apparently that is special - found what I needed here:

http://www.xmlplease.com/xhtmlxhtml

Argh!
george
Site Admin
Posts: 2097
Joined: Thu Jan 09, 2003 2:58 pm

Re: simple identity transform - what am I missing?

Post by george »

The problem is that your template matches div from no namespace while your document contains div in the http://www.w3.org/1999/xhtml namespace. The simplest way, as you use XSLT 2.0, is to declare the default XPath namespace adding xpath-default-namespace="http://www.w3.org/1999/xhtml" on the stylesheet

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="http://www.w3.org/1999/xhtml">
...
Best Regards,
George
George Cristian Bina
Post Reply