Alexander Zhukov
Alexander Zhukov

Reputation: 4547

Cannot apply XSLT template to xml element

I have the following piece of XML:

<?xml version="1.0"?>
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml" id="new" cnxml-version="0.7" module-id="new">

<metadata xmlns:md="http://cnx.rice.edu/mdml"
      mdml-version="0.5">

    <md:abstract>By the end of this section, you will be able to:
        <list>
            <item>Discuss the role of homeostasis in healthy functioning</item>
            <item>Contrast negative and positive feedback, giving one physiologic example of each mechanism</item>
        </list>
    </md:abstract>

and I need to apply template to md:abstract element. I have the following XSL code:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:md="http://cnx.rice.edu/mdml/0.4">

<xsl:output omit-xml-declaration="yes" encoding="ASCII"/>

<xsl:template match="c:document">
<body>
    <xsl:apply-templates select="c:metadata"/>
    <xsl:apply-templates select="c:content"/>
</body>
</xsl:template>

<xsl:template match="c:metadata">
    <xsl:apply-templates select="md:abstract"/>
</xsl:template>

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

but , unfortunately, it doesn't capture md:abstract element. I cannot figure out, want is wrong.

Upvotes: 1

Views: 129

Answers (1)

peter.murray.rust
peter.murray.rust

Reputation: 38043

In your document you have xmlns:md="http://cnx.rice.edu/mdml" whereas in the XSLT you have xmlns:md="http://cnx.rice.edu/mdml/0.4". These are not the same. Namespaces must be lexically identical to match (there is no aliasing or superclassing).

I've been through this myself, trying to manage versioned namespaces, and it's a nightmare. Either it fails as in your example or you end up checking against all versions explicitly. If it's your namespace, remove the version.

If you are designing it, you can always put the version in a separate attribute (as XSLT transformations do).

Upvotes: 2

Related Questions