Click to See Complete Forum and Search --> : XSLT Template Matching Problem


Jim_Cross
October 29th, 2003, 04:49 PM
Hi,

I'm currently trying to use XSLT to reformat an XML file.
The XML file is as follows:

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

<schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://dbnexus.risk.db.com/schema/DM_DB_INDUSTRY_NACE_CODES" version="1" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:dbn="http://dbnexus.risk.db.com/schema/dbnexus">
<import namespace="http://dbnexus.risk.db.com/schema/dbnexus" schemaLocation="http://dbnexus.risk.db.com/schema/dbnexus.xsd"/>
<element name="DM_DB_INDUSTRY_NACE_CODES">
<annotation>
<appinfo>
<uniqueconstraint>
<xpath value="/DM_DB_INDUSTRY_NACE_CODES/DB_INDUSTRY_CODE"/>
</uniqueconstraint>
<hierarchydisplay>
<xpath value="/DM_DB_INDUSTRY_NACE_CODES/DB_INDUSTRY_CODE"/>
<xpath value="/DM_DB_INDUSTRY_NACE_CODES/BUBA_NACE"/>
<xpath value="/DM_DB_INDUSTRY_NACE_CODES/NACE_DESC"/>
</hierarchydisplay>
<index>
<xpath value="/DM_DB_INDUSTRY_NACE_CODES/DB_INDUSTRY_CODE"/>
<xpath value="/DM_DB_INDUSTRY_NACE_CODES/BUBA_NACE"/>
<xpath value="/DM_DB_INDUSTRY_NACE_CODES/NACE_DESC"/>
</index>
<defaultsort>
<xpath value="/DM_DB_INDUSTRY_NACE_CODES/DB_INDUSTRY_CODE"/>
</defaultsort>
<shortname>DM_DB_INDUSTRY_NACE_CODES</shortname>
</appinfo>
</annotation>
<complexType>
<sequence>
<!-- append user attributes and elements here -->
<element maxOccurs="1" minOccurs="1" name="DB_INDUSTRY_CODE" nillable="false">
<simpleType>
<restriction base="string">
<minLength value="1"/>
<maxLength value="10"/>
</restriction>
</simpleType>
</element>
<element maxOccurs="1" minOccurs="1" name="BUBA_NACE" nillable="false">
<simpleType>
<restriction base="string">
<minLength value="1"/>
<maxLength value="3"/>
</restriction>
</simpleType>
</element>
<element maxOccurs="1" minOccurs="1" name="NACE_DESC" nillable="false">
<simpleType>
<restriction base="string">
<minLength value="1"/>
<maxLength value="255"/>
</restriction>
</simpleType>
</element>
</sequence>
<attributeGroup ref="dbn:versionAttributes"/>
<attributeGroup ref="dbn:idAttributes"/>
</complexType>
</element>
</schema>



What I want to do is firstly select the first element, so something with it, the do something different to all the sub-elements.

However, I can't seem to match the elements in the document. For example, the following code simply prints out "Found node!" a lot, without ever printing "Found element":


<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<head>
</head>
<body>
<xsl:apply-templates></xsl:apply-templates>
</body>
</html>
</xsl:template>

<xsl:template match="node()">
<xsl:text>Found node!</xsl:text>
<xsl:text> | </xsl:text>
<xsl:apply-templates></xsl:apply-templates>
</xsl:template>


<xsl:template match="element">
<xsl:text>Found element!</xsl:text>
<xsl:apply-templates></xsl:apply-templates>
</xsl:template>

</xsl:stylesheet>


The same happens if I try to match "schema", "schema/element" etc.

Any suggestions as to where I'm going wrong?

Thanks

Jim

erwin_78
October 30th, 2003, 04:59 AM
try to use <xsl: apply-templates select="."/> (dot equals current node)
because when applying you need to pass a value to which it applies to.

khp
October 30th, 2003, 06:19 AM
Originally posted by erwin_78
try to use <xsl: apply-templates select="."/> (dot equals current node)


I think this is a rather dangerous suggestion :). if you do this in the match="node()" template, you will get an infinite loop :eek:

The default behaviour of the apply-templates element is to match all child nodes. And this works fine for Jim, otherwise he wouldn't get any output at all.

The problem is caused by the namespace declarations.

In the xml file the root namespace is declared to be xmlns="http://www.w3.org/2001/XMLSchema"

So the stylesheet needs to match this namespace. Put the namespace decleration xmlns:xs="http://www.w3.org/2001/XMLSchema"
in the stylesheet.
And then match "xs:element" instead of simply "element", in the template decleration.