473,398 Members | 2,389 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,398 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 2312
"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.