Groups | Blog | Home
all groups > dotnet xml > september 2004 >

dotnet xml : <xsl:if> construct



Derek Harmon
9/12/2004 2:58:55 AM
[quoted text, click to view]

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

Luke Vogel
9/12/2004 3:41:15 PM
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=3D"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? =20

I'm totally lost ...=20

TIA
--=20
Regards Luke.
-----
There are 10 types of people in this world
Those that understand binary and those that don't
Derek Harmon
9/12/2004 4:10:30 PM
[quoted text, click to view]

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

Luke Vogel
9/12/2004 6:30:57 PM
Hi Derek,

[quoted text, click to view]

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
------
[quoted text, click to view]
0 rows returned
------

Luke Vogel
9/12/2004 7:58:47 PM
Hi Derek,

[quoted text, click to view]

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.
------

Luke Vogel
9/13/2004 6:08:25 PM
Thanks Derek ...

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

Again, thanks for the prompt reply.

--
Regards
Luke
------
2b|~2b == -1
------

AddThis Social Bookmark Button