XSLT and default templates.

I am new to XML/XSLT. I was working through a tutorial and got stuck on a problem. Could someone please explain the following.

1) XSLT has default templates that it uses if you do not provide a template that matches against the "documents root".

They are:

<xsl:template match="*|/">
<xsl:apply-templates/>
</xsl:template>

and

<xsl:template match="text()|@*">
<xsl:value-of select="."/>
</xsl:template>

If I create an xslt document with no templates and run it against an XML document, I should see all element and attribute values.

When I tested it out, I got the element values, but not the attribute values.

The following is my two documents that I ran using the command line "msxsl.exe".

--XML DOC--
<?xml version="1.0"?>
<order OrderNumber="312597">
<date>2000/1/13</date>
<customer id="A216">Company A</customer>
<item>
<part-number warehouse="Warehouse 11">E16-25A</part-number>
<description>Production-Class Widget</description>
<quantity>16</quantity>
</item>
<item>
<part-number warehouse="Warehouse 12">E17-25A</part-number>
<description>Production-Class Widget</description>
<quantity>16</quantity>
</item>
</order>

--XSL DOC--
<?xml version="1.0"?>
<x:stylesheet version="1.0"
xmlns:x="http://www.w3.org/1999/XSL/Transform">
<x:output method="text" encoding="UTF-8"/>

</x:stylesheet>
[1729 byte] By [llass61] at [2007-11-18 18:34:16]
# 1 Re: XSLT and default templates.
Originally posted by llass61
If I create an xslt document with no templates and run it against an XML document, I should see all element and attribute values.

No, at no place in the above templates, does it say output element tags and attributes. The first templates says 'recursivly select all elements without outputting anything. The second one says, output the contents of selected text and attribute nodes, the contents of the attributes never gets outputted because they are never selected.

To get the element tags, I think you could use a template like:

<xsl:template match="*">
<xsl:copy-of select=".">
<xsl:apply-templates/>
</xsl:copy-of>
</xsl:template>


Unfortunatly I can't test this right now.
khp at 2007-11-10 3:28:15 >
# 2 Re: XSLT and default templates.
But why do the "Elements" get output?

The first template will "recursively select all elements without outputting anything". Does this include text nodes? Or just elements.

If it does not include text nodes, then how are the elements text values being output. They should not match to the second template (it should only match text nodes and attribute nodes ("text()|@*").

I guess I'm confused maybe on what a text node is and how XSLT matches the elements for outputting using the default templates.

Thanks for any help you can provide!
llass61 at 2007-11-10 3:29:16 >
# 3 Re: XSLT and default templates.
Originally posted by llass61
If it does not include text nodes, then how are the elements text values being output.

The first template only matches element nodes, not text and attributes, but it selects both elements nodes and text nodes to be matched.

<xsl:apply-templates/>
is really shorthand for
<xsl:apply-templates select="*|text()"/>

The selected text nodes are then matched by the second template, which produces the output.
khp at 2007-11-10 3:30:20 >