[xsl] understanding attributes and predicates

Subject: [xsl] understanding attributes and predicates
From: Terry Ofner <tofner@xxxxxxxxxxx>
Date: Fri, 1 Feb 2008 12:59:20 -0500
Input document structure:

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

  <part>
    <par class="title">Unit 1: Vocabulary Skills</par>
    <par class="testType">Posttest</par>
    <section level="1">
      <heading class="heading 1" level="1"></heading>
      <par class="stem_mc"Read the sentence. Then choose the best
synonym to replace the boldfaced word.</par>
      <par class="display">The threat of rain makes i. . .</par>
      <par class="choice-a">doubtful</par>
      <par class="choice-b">likely</par>
      <par class="choice-c">hopeful</par>
      <par class="choice-d">obvious</par>
      <par class="Answer">A</par>
      <par class="g_code">G5U1S5</par>
    </section>

    <section level="1">
      <heading class="heading 1" level="1"></heading>
      <par class="stem_sa">;Describe the connotation of the word
mutt and 8xplain whether it is positive or negative. </par>
      <par class="Answer">Answers will vary. Possible answer: The
word <inline style="font-style: italic;">mutt</inline> has the
negative connotation of a dog that isnt worth very much or that
isnt good-looking.</par>
      <par class="g_code">G5U1S8</par>
       </section>
  </part>
</document>

I have been running the following stylesheet as a learning tool. I
adjust the sheet and examine the output.
With this template I get a list of attribute names and values:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
	xmlns:xlink="http://www.w3.org/1999/xlink";
	xmlns:html="http://www.w3.org/HTML/1998/html4";
	exclude-result-prefixes="html xlink">
	<xsl:preserve-space elements="*"/>

<xsl:template match="par">
  <!-- List the attribute names and values. -->
<xsl:for-each select='@*'>
  attribute name: <xsl:value-of select="name()"/>
attribute value: <xsl:value-of select="."/>
</xsl:for-each>
  </xsl:template>
  </xsl:stylesheet>

Like this:

  attribute name: class
attribute value: title

  attribute name: class
attribute value: testType

  attribute name: class
attribute value: stem_mc

  attribute name: class
attribute value: display


When I select specific par elements with a predicate, as in the template below, I get the attribute name, attribute value, and the text node of the <par> element.

<xsl:template match="par[@class='stem_mc']">
  <!-- List the attribute names and values. -->
<xsl:for-each select='@*'>
  attribute name: <xsl:value-of select="name()"/>
attribute value: <xsl:value-of select="."/>
</xsl:for-each>
  </xsl:template>


I just wonder what is going on. Is it the "for-each select='@*'" or the "value-of select="." that is returning the text node of the par element?

Terry

Current Thread