Re: Possible to use attribute value in pattern ??

Subject: Re: Possible to use attribute value in pattern ??
From: "Pete Johnston" <P.Johnston@xxxxxxxxxxxxxxxxxx>
Date: Fri, 16 Jul 1999 10:08:48 GMT
[Caleb Drake]
> Suppose the source document has:
> <book title="All About Foo" author="Dr. Quux"/>
> and I want to place the title in one part of the result tree and the author
> in another.  To do so, I might have
> <xsl:apply-templates select="book[@title]"/>
> in one place, and
> <xsl:apply-templates select="book[@author]"/>
> in the other.  The problem is that both apply-templates "calls" match the
> same
> source element so only one of them gets generated.  In addition, the
> template
> rule triggered by these two apply-templates tags cannot distinguish which
> apply-templates "call" triggered the rule, so it doesn't know which
> attribute to style

I'm not sure I have understood your problem correctly, but my 
interpretation is that your two apply-templates elements here are 
selecting (respectively) the book element which has a title attribute 
and the book element which has an author attribute - which in 
your case turn out to be the same element, and so the same 
(match="book"?) template is instantiated. 

But it sounds like what you want is to select _separately_ the author 
attribute of the book element and the title attribute of the book 
element (rather than the book element itself). This requires a 
different pair of patterns:

<xsl:apply-templates select="book/@title"/>
<xsl:apply-templates select="book/@author"/>

I think this is the point David Carlisle was illustrating with his 
three examples yesterday.

For example with this document

<?xml version="1.0" ?>
<list>
<book title="Neuromancer" author="William Gibson">some content about
Neuromancer</book>
<book title="Snow Crash" author="Neal Stephenson">some content about 
Snow Crash</book>
</list>

the stylesheet

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

<xsl:template match="list">

<xsl:apply-templates select="book[@title]"/>
<xsl:apply-templates select="book[@author]"/>

</xsl:template>

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

</xsl:stylesheet>

produces a rather different result from

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

<xsl:template match="list">

<xsl:apply-templates select="book/@title"/>
<xsl:apply-templates select="book/@author"/>

</xsl:template>

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

</xsl:stylesheet>

Many apologies if I'm missing the point you are making!

Pete Johnston
University of Glasgow


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread