XSLT natch (xsl:template matching)

K

Kofi Sarfo

Wondering what I'd use to evaluate the following to return 'Easy'

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="Answers.xsl"?>
<answers>
<answer>
<code>A</code>
<method>Easy</method>
</answer>
<answer>
<code>B</code>
<method>Way</method>
</answer>
<answer>
<code>C</code>
<method>Out</method>
</answer>
</answers>

The following "Answers.xsl" doesn't do it:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/answers/answer
Code:
">
<xsl:value-of select="lang_description" />
</xsl:template>
</xsl:stylesheet>

And while we're at it... why does the evaluated expression differ for
the following?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/answers/answer[code='A']/lang_description">
<xsl:value-of select="." />
</xsl:template>
</xsl:stylesheet>

Thanks
 
D

Dimitre Novatchev

There's no "lang_description" element in the xml source provided by you.

The result you want can be expressed as the following XPath expression:

/answers/answer[code = 'A'][1]/method/text()


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
 
K

Kofi Sarfo

You're right. "lang_description" should have read "method" but then
the example XML differs from the one I'm actually working with and the
principle (syntax) still fails to give what I'm looking for.

In fact I think I've already tried the XSL you suggested with the
exception of the [1]. Position is irrelevant and there are unique
codes only.

Anybody?

<!-- XML FILE -->

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="TransColour.xsl"?>
<all_colours>
<colour>
<code>B</code>
<lang_description>Navy</lang_description>
</colour>
<colour>
<code>R</code>
<lang_description>Maroon</lang_description>
</colour>
<colour>
<code>C</code>
<lang_description>Charcoal</lang_description>
</colour>
<colour>
<code>G</code>
<lang_description>Grey</lang_description>
</colour>
<colour>
<code>K</code>
<lang_description>Black</lang_description>
</colour>
</all_colours>

<!-- XSL FILE -->

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/all_colours/colour
Code:
[1]/lang_description/text()">
<xsl:value-of select="." />
</xsl:template>
</xsl:stylesheet>

I feel I really should know how to do this by now and hang head in
shame.

http://www.single-blend.net/dotnet/

[QUOTE="Dimitre Novatchev"]
There's no "lang_description" element in the xml source provided by you.

The result you want can be expressed as the following XPath expression:

/answers/answer[code = 'A'][1]/method/text()


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL


[QUOTE="Kofi Sarfo"]
Wondering what I'd use to evaluate the following to return 'Easy'

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="Answers.xsl"?>
<answers>
<answer>
<code>A</code>
<method>Easy</method>
</answer>
<answer>
<code>B</code>
<method>Way</method>
</answer>
<answer>
<code>C</code>
<method>Out</method>
</answer>
</answers>

The following "Answers.xsl" doesn't do it:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/answers/answer[code='A']">
<xsl:value-of select="lang_description" />
</xsl:template>
</xsl:stylesheet>

And while we're at it... why does the evaluated expression differ for
the following?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/answers/answer[code='A']/lang_description">
<xsl:value-of select="." />
</xsl:template>
</xsl:stylesheet>

Thanks[/QUOTE][/QUOTE]
 
K

Kofi Sarfo

"method" exists as "lang_description" in the XML file I'm trying to
transform.

/answers/answer[code = 'A'][1]/method/text()

didn't work for me either. I'm using the MSXML4 parser. That shouldn't
be a problem, should it?

Cheers

K

http://www.single-blend.net/dotnet/
 
O

Oleg Tkachenko

Julian said:
And the alternative would be...?
What's the catch? :)
Isn't the following more effective and maintainable one?

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates
select="/all_colours/colour
Code:
[1]/lang_description" />
</xsl:template>
<xsl:template match="lang_description">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
 
O

Oleg Tkachenko

Julian said:
I'm not sure why this would be more efficient? Can you explain and/or
did you measure it?

Well, I believe 11 applying templates against
"/all_colours/colour
Code:
[1]/lang_description/text()" pattern is anyway
worse than matching of root node, evaluation of
/all_colours/colour[code='B'][1]/lang_description and matching of
lang_description element.
 
O

Oleg Tkachenko

Julian said:
I think you are making a lot of assumptions about how the XSLT processor
works. Unless you really know *or* indeed have measured that, I just
don't believe you :)
Well, may be I'm really thinking too much in terms of how *average* processor
works. That's funny, as a matter of fact I do think too much how it works and
how it should work, because this week I have started to work on Mono managed
XSLT ;)
Anyway, according to my rough measurements with original XML running MSXML4 I
got approximately 0.390ms for original stylesheet #1 and 0.340 for my
stylesheet #2.
*But* having increased source XML to 50kb I got 6.5ms vs 0.340ms!
That actually perfectly makes sense - my stylesheet still makes only 2 apply
templates and one query, while original stylesheet gone nuts with that
superfluous matching.

stylesheet #1:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/all_colours/colour
Code:
[1]/lang_description/text()">
<xsl:value-of select="." />
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>

stylesheet #2:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates
select="/all_colours/colour[code='B'][1]/lang_description" />
</xsl:template>
<xsl:template match="lang_description">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
 
J

Julian F. Reschke

Oleg said:
Julian said:
I think you are making a lot of assumptions about how the XSLT
processor works. Unless you really know *or* indeed have measured
that, I just don't believe you :)

Well, may be I'm really thinking too much in terms of how *average*
processor works. That's funny, as a matter of fact I do think too much
how it works and how it should work, because this week I have started to
work on Mono managed XSLT ;)
Anyway, according to my rough measurements with original XML running
MSXML4 I got approximately 0.390ms for original stylesheet #1 and 0.340
for my stylesheet #2.
*But* having increased source XML to 50kb I got 6.5ms vs 0.340ms!
That actually perfectly makes sense - my stylesheet still makes only 2
apply templates and one query, while original stylesheet gone nuts with
that superfluous matching.

stylesheet #1:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template
match="/all_colours/colour
Code:
[1]/lang_description/text()">
<xsl:value-of select="." />
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>

stylesheet #2:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates
select="/all_colours/colour[code='B'][1]/lang_description" />
</xsl:template>
<xsl:template match="lang_description">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>[/QUOTE]

Interesting. Can you repeat that test with Saxon?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,148
Latest member
ElizbethDa
Top