namespace problem when generating HTML

Here should go questions about transforming XML with XSLT and FOP.
alk
Posts: 32
Joined: Wed Jan 25, 2006 5:43 pm

namespace problem when generating HTML

Post by alk » Wed Feb 01, 2006 5:55 pm

Hi,

I'm trying to generate HTML out of an XML doccument - so far, so good. The problem is that the XML document uses a namespace defined in the related XSD.
When I remove the namespace information from the XML file the HTML file is generated, otherwise the elements are not recognized and the output remains empty.

Any idea on how to make the namespace and thus the elements available in the XSLT? Just adding the same information as in the XML file does not do the trick.

Best regards
- Alex

george
Site Admin
Posts: 2101
Joined: Thu Jan 09, 2003 2:58 pm

Post by george » Wed Feb 01, 2006 7:06 pm

Hi Alex,

You need to change the name tests to use the prefix. For instance if you have something like

<xsl:template match="test">

that will match the test element from no namespace. As you have in your document test in some namespace then you need to use somthing like

<xsl:template match="x:test">

where x is mapped to your element namespace.

If you are using XSLT 2.0 adding an attribute:
xpath-default-namespace="yourNamespaceHere"
on your xsl:stylesheet element should be enough as support for default namespace was added starting with XSLT 2.0.

Best Regards,
George

alk
Posts: 32
Joined: Wed Jan 25, 2006 5:43 pm

Post by alk » Thu Feb 02, 2006 10:22 am

Hi George,

thanks for you answer. The problem is that my namespace has no prefix that I could add. All my XSD stuff has MathML as namespace (xmlns="http://www.w3.org/1998/Math/MathML"). When I remove this from the XML file the transformation works.

xpath-default-namespace I had already tried with little success.

My XML file starts like this:

<top xmlns="http://www.w3.org/1998/Math/MathML" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/1998/Math/MathML XSD\myxsd.xsd">

the XSLT looks like this:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://www.w3.org/1998/Math/MathML XSD\myxsd.xsd">

Any idea???

Cheers
- Alex

george
Site Admin
Posts: 2101
Joined: Thu Jan 09, 2003 2:58 pm

Post by george » Thu Feb 02, 2006 10:31 am

Hi Alex,

xpath-default-namespace="http://www.w3.org/1998/Math/MathML XSD\myxsd.xsd">

The namespace is only the first part, that is http://www.w3.org/1998/Math/MathML. The second part is the schema document and in general that is used as a hint for the XML Schema processor but that is not part of the namespace. Try using:

xpath-default-namespace="http://www.w3.org/1998/Math/MathML"

Best Regards,
George

alk
Posts: 32
Joined: Wed Jan 25, 2006 5:43 pm

Post by alk » Thu Feb 02, 2006 11:08 am

Hi George,

thanks again for the very fast answer and yes now it works!
I thought I had tried without the path information as well, but it didn't work - must have mistaken...

Best regards
- Alex

mphare
Posts: 71
Joined: Fri Apr 30, 2004 8:00 pm
Location: Texas

Post by mphare » Fri Feb 17, 2006 11:15 pm

Hi,

I've got a similar problem, but your solution didn't seem to help me.

I'm trying to parse the output from OpenOffice, here's a paired down example:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?><?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:x2="http://schemas.microsoft.com/office/excel/2003/xml"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:html="http://www.w3.org/TR/REC-html40"
xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet">

<Cell>Inside the Cell 1</Cell>
<Cell>Inside the Cell 2</Cell>
</Workbook>
Right now I'd be happy just to xpath into the 'Cell' element, so my xsl looks like:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xpath-default-namespace="urn:schemas-microsoft-com:office:spreadsheet">
<xsl:template match="/Workbook">
<p>AAAAAA</p>
<xsl:apply-templates select="Cell"></xsl:apply-templates>

</xsl:template>

<xsl:template match="Cell">
My Cell
</xsl:template>
</xsl:stylesheet>
However my output is not what I would expect.

All I get is the default template processing for the Cell elements

Code: Select all

<?xml version="1.0" encoding="utf-8"?>

Inside the Cell 1
Inside the Cell 2
Anyone have better eyes than me and can see what I've left out?

I should add, if I remove the default namespace (or give it a prefix) then the processing works just as I would expect it would.

Code: Select all

<?xml version="1.0" encoding="utf-8"?><p>AAAAAA</p>
My Cell

My Cell
It is the default namespace in the XML file that's causing me fits.

Thanks,
--------------------------

- mike

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

mphare
Posts: 71
Joined: Fri Apr 30, 2004 8:00 pm
Location: Texas

Post by mphare » Sat Feb 18, 2006 12:16 am

OK, never mind. It works using Saxon 8.1B, but not Saxon 6.5.3 or Xalan.

Thanks!
--------------------------

- mike

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

george
Site Admin
Posts: 2101
Joined: Thu Jan 09, 2003 2:58 pm

Post by george » Sat Feb 18, 2006 9:04 am

Hi,

Yes, because this works in XSLT 2.0 and not in XSLT 1.0. In XSLT 1.0 there is no notion of default namespace. And both Saxon 6 and Xalan are XSLT 1.0 processors while Saxon 8 is an XSLT 2.0 processor.

Best Regards,
George

Post Reply