473,324 Members | 2,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

XSLT natch (xsl:template matching)

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
Jul 20 '05 #1
7 1746
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
"Kofi Sarfo" <ko*******@hotmail.com> wrote in message
news:e9**************************@posting.google.c om...
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

Jul 20 '05 #2
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='B'][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/

"Dimitre Novatchev" <dn********@yahoo.com> wrote in message news:<bh*************@ID-152440.news.uni-berlin.de>...
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
"Kofi Sarfo" <ko*******@hotmail.com> wrote in message
news:e9**************************@posting.google.c om...
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

Jul 20 '05 #3
"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/

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #4
Julian F. Reschke wrote:
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='B'][1]/lang_description" />
</xsl:template>
<xsl:template match="lang_description">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Jul 20 '05 #5
Julian F. Reschke wrote:
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='B'][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.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Jul 20 '05 #6
Julian F. Reschke wrote:
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='B'][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>
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Jul 20 '05 #7
Oleg Tkachenko wrote:
Julian F. Reschke wrote:
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='B'][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>


Interesting. Can you repeat that test with Saxon?

Jul 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Vijay singh | last post by:
Hi wonder if anybody can clear by doubt XML file : <score id="1"> <film>A Little Princess</film> <composer>Patrick Doyle</composer>
2
by: Soren Kuula | last post by:
Hi, Suppose I have: <xsl:template match="foo"/> <xsl:template match="foo"/> -- is that an error (same priority)? (my precessor reports nothing, buts as far as I recall it doesn't really...
5
by: KJ | last post by:
I would like to know if there is a tool for download or purchase that will generate a skeleton xslt document, inserting all the xsl:template's matching each element specified in any given xsd. ...
4
by: dwa | last post by:
Is it legal to use a parameter in a <template match> ? If I do the following: <xsl:template match="/ContentRoot/Content/Categories/Category"> ....everything works as expected. But if I...
3
by: Steve | last post by:
Is there any way of specifying the startMode when using the xslTransform class? We are updating code which used msxml to the system.xml classes but can find no way to specify the startMode. We...
2
by: tentstitcher | last post by:
Hello: I am stumped on what I belive to be a namespace issue. The XSL, source XML, and result XML are included below. The problem is that the template to match "getBalances" is never invoked by...
2
by: daz_oldham | last post by:
If I have the following XML: <xml> <levelone> <leveltwo attribute="value"> <levelthree attribute2="value2"> Some Value </levelthree> </leveltwo> </levelone>
2
by: eric.goforth | last post by:
Hello, What happens if you call an xsl template if you call it without setting the values of all it's parameters, I assume that the parameters will be either null or an empty string and the...
4
by: festo | last post by:
Hi guys, I need to print a different footer depending on the template being printed. How do I test for the current template being processed in XSLT? Thanks, fes
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.