468,531 Members | 1,640 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,531 developers. It's quick & easy.

<xsl:if> construct

Hi all,

Probably a really basic question, but I cant find an answer ...

I have an xml file of books something like:
<product>
<isbn>0-735-61374-5</isbn>
<title>Microsoft Visual Basic Step By Step</title>
<author>Michael Halvorsen</author>
<subject>Programming</subject>
<blurb>Here's a fast way for any programmer to begin ... yada yada .....</blurb>
<price>89.95</price>
<category>Computers</category>
<image>images/comp-MSVB_SBS.jpg</image>
</product>

I need to be able to extract only books that belong to a specific category (in the example above 'Computers')

I've been looking at the <xsl:if test="expression"> construct, but I cant figure out the syntax for the test expression.
I keep getting "invalid token" errors, or something about being part of a dataset ...

Can anyone give me a few pointers?

I'm totally lost ...

TIA
--
Regards Luke.
-----
There are 10 types of people in this world
Those that understand binary and those that don't
-----
Nov 12 '05 #1
5 2144
"Luke Vogel" <not@_real_address> wrote in message news:eQ*************@TK2MSFTNGP09.phx.gbl...
<category>Computers</category>
<image>images/comp-MSVB_SBS.jpg</image>
</product>

I need to be able to extract only books that belong to a specific category (in the example above 'Computers')

I've been looking at the <xsl:if test="expression"> construct, but I cant figure out the syntax for the test expression.
I keep getting "invalid token" errors, or something about being part of a dataset ...


The exact expression depends on the context node, if you are in a template matching product then
it's simply: category='Computers'

e.g.,

<xsl:template match="product" >
<xsl:if test="category='Computers'">
<!-- Do something with this product node. -->
</xsl:if>
</xsl:template>

Notice that since the test attribute is delimited in double quotes, the text literals within the TestExpr
should be delimited in single quotes.

If this is all your template is doing, however, you could make it match more specifically and do
w/o an <xsl:if> altogether,

<xsl:template match="product[category='Computers']" >
<!-- Do something with product nodes where their category is Computers. -->
</xsl:template>

<xsl:template match="product" >
<!-- Do nothing with product nodes of other categories. -->
</xsl:template>

<xsl:template match="/">
<products>
<xsl:apply-templates select="products/product" />
</products>
</xsl:template>

This uses a predicate, to make the one product-matching template more specific than the
others. For instance, the stylesheet may generate an HTML table, and style the rows
containing book records in the Computers category in cyan while others remain white.

(I've assumed in this last XSLT example snippet that you do have some containing document
element, like products, because of the requirement that there can be only one root element
and presumably there are multiple products so they must be children.)
Derek Harmon
Nov 12 '05 #2
Hi Derek,

"Derek Harmon" <lo*******@msn.com> wrote in message
news:O1**************@TK2MSFTNGP12.phx.gbl...
it's simply: category='Computers'

e.g.,

<xsl:template match="product" >
<xsl:if test="category='Computers'">
<!-- Do something with this product node. -->
</xsl:if>
</xsl:template>
This worked perfectly. I could swear I tried this, but ended up with an
error of some sort ... anyway, thanks for your prompt reply.
--
Regards
Prime
------ SELECT * FROM users WHERE clue > 0

0 rows returned
------
Nov 12 '05 #3
Hi Derek,

"Derek Harmon" <lo*******@msn.com> wrote in message
news:O1**************@TK2MSFTNGP12.phx.gbl...
The exact expression depends on the context node, if you are in a template matching product then it's simply: category='Computers'

e.g.,

<xsl:template match="product" >
<xsl:if test="category='Computers'">
<!-- Do something with this product node. -->
</xsl:if>
</xsl:template>


This worked fine (as per my last message to you, got another question
now ...

How would I go about passing a text value to the xsl transform so that I
can search my xml catalog for that string, and what would be the best
way of doing the search ... using the contains() function perhaps?

TIA

Luke.
--
Regards
Luke
------
Sometimes my mind wanders.. other times it packs it's suitcase and
goes away for weeks at a time.
------
Nov 12 '05 #4
"Luke Vogel" <not@_real_address> wrote in message news:u$**************@TK2MSFTNGP10.phx.gbl...
How would I go about passing a text value to the xsl transform so that I
can search my xml catalog for that string, and what would be the best
way of doing the search ... using the contains() function perhaps?


Pass an argument (in an XsltArgumentList) into the XslTransform's Transform( ) that can be
received as a global <xsl:param> of the <xsl:stylesheet>. Then reference it as you would
an XSLT variable (using a leading $) within the stylesheet, $myArgument.

// in C# source file ...
XsltArgumentList args = new XsltArgumentList( );
args.AddParam( "myArgument", string.Empty, "someTextToLookFor");
stylesheet.Transform( doc.CreateNavigator( ), args, new XmlTextWriter( Console.Out));

// in .xsl stylesheet ...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="myArgument">defaultTextIfNoArgumentSupplied</xsl:param>
: :
<xsl:template match="/">
<!-- when called as above, inserts 'someTextToLookFor' text node into result document. -->
<xsl:value-of select="$myArgument" />
</xsl:template>
: :
</xsl:stylesheet>

As for performing a full text-search, that depends on the complexity of your XML. If you
are using contains( ), starts-with( ) or substring( ) outside of narrowly-confined contexts,
performance suffers. IMO, picking apart text nodes isn't a forte of XPath 1.0, it's strength
is in it's path expressions to identify element and attribute nodes from a node-set (text nodes
are the least desirable place to store information you want to make readily exposed to these
expressions).

If I were serious about searching in an efficient manner, I'd extract the info out of the XML
document and create indexed, searchable abstract data structures to do the searches myself
(or more likely import the data into SQL Server to use it's full text-search capabilities).
Derek Harmon
Nov 12 '05 #5
Thanks Derek ...

I'll need a little time to digest this information ...

Again, thanks for the prompt reply.

--
Regards
Luke
------
2b|~2b == -1
------
Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by websls | last post: by
3 posts views Thread by Blaise Garant | last post: by
5 posts views Thread by ina | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.