Zeath
Zeath

Reputation: 1

XSL template doesn't recognize element

I'm new to xml and xsl and i struggling to get my xsl file working. I have this xml:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="recipes-to-html.xsl"?>
<?dsd href="recipes.dsd"?>
<collection xmlns="http://recipes.org"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://recipes.org recipes.xsd">
</collection>

And i have this xsl file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:r="http://recipes.org">

    <xsl:output method="xml"
                doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
                doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
                encoding="UTF-8"
                indent="yes"
    />
    <xsl:template match="collection">
        <html>
            <head>
                <title>Collection of Recipies</title>
            </head>
            <body>
                <h1>Collection of Recipies</h1>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

The xml file of course includes much more but im struggling here already. When i open the xml file nothing shows and that suggests to me that it's not registering the collection element. But to my knowledge I'm doing everything right.

Can someone please help me with this?

EDIT: I've tried xsltproc and it it says it fails to parse the file. When i replace the "collection" in match in xslt file to "/" it works. I don't know what could couse that.

Upvotes: 0

Views: 359

Answers (2)

Daniel Haley
Daniel Haley

Reputation: 52858

You've already bound the default namespace http://recipes.org to the prefix r; you just need to use it in your XPath now...

<xsl:template match="r:collection">
    <html>
        <head>
            <title>Collection of Recipies</title>
        </head>
        <body>
            <h1>Collection of Recipies</h1>
        </body>
    </html>
</xsl:template>

You should probably also add exclude-result-prefixes="r" so the unneeded/unwanted namespace is excluded.

Fiddle: http://xsltfiddle.liberty-development.net/jyH9rLY/2

Upvotes: 0

Martin Honnen
Martin Honnen

Reputation: 167581

Use xpath-default-namespace="http://recipes.org" on your xsl:stylesheet element to ensure your path expressions and match patterns not using any prefix in names (e.g. collection) select or match elements in that default namespace of the XML input document.

Upvotes: 1

Related Questions