472,986 Members | 2,825 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

<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 2291
"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 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: websls | last post by:
I tried to do this : <xsl:if test="ToutCompris"> some output </xsl:if> ToutCompris is a boolean element in my XML file My problem is the output is parse even when ToutCompris is false I...
3
by: Blaise Garant | last post by:
Hi I've made a stylesheet to transform my data into XSL-FO. This stylesheet used to work with MSXSL 4.0 but I've got some issues in ..NET. First, I changed removed all the "node-set()" function...
3
by: tldisbro | last post by:
Hello All, I am trying to use the returned value of the <fo:page-number> element/function in my <xsl:if> test condition. But am unsuccessful in doing so. Is it possible to use it in this fashion...
5
by: ina | last post by:
Hello guys, My name is ina and I have a problem with a file xlst. I am newbie and sorry for this question, probably must be very simple. I have this xml file <?xml version="1.0"?>...
2
by: riceyeh | last post by:
Hi, What does <xsl:if test="not($values)"mean? What I do not understand is $values? Here, means array? And . = $value means current node is equal to the variable value? So the total meaning is...
5
by: jhurrell | last post by:
I have been having some trouble getting my XSL style sheet to parse correctly. I have some XML outputted from an SQL-Server, that I then need to turn into multiple HTML files. This I have done...
4
by: mark4asp | last post by:
I'm getting a problem with this code and I think the offending linke is : <xsl:if test="$folder = 'Search'"> I want to test the value of the Folder element for a value of precisely "Search"...
8
by: Hoi Wong | last post by:
With the XSLT 1.0 engine that I was forced to use, I have to parse old XML scripts where the number (to be parsed and saved into $EPISODE_NUMBER_RAW) that I want to parse is written with a comma...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.