473,659 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use XPath to select element with text match

Hello,

I've been reading up on xpath and I am able to access elements with it.
I haven't been able to figure one thing out though.

How would I use XPath to select an element where the text equals what
I'm looking for?

If I select a book topic (in the traditional of XML examples) of
Programming and it returns 200 books, and I want to specifically select
books with an element named "topic" text of "C++" what would the xpath
be?

<books>
<category>
<topic>Linux</topic>
<title>Linux ABC's</title>
<desc>The ABC's of Linux</desc>
<topic>C++</topic>
<title>Beginnin g C++</title>
<desc>C++ programming for beginners</desc>
<topic>C++</topic>
<title>Patter ns in C++</title>
<desc>Advance d C++ programming</desc>
</category>
</books>

If I wanted to only select the elements in category matching the topic
text of C++, how would I use XPath to achieve this?

I thought it would be something like //books/category[topic='C++'] but
I get a node test expected error. I'm adding XML support to a VB
application and using MSXML6, but I don't think that's relevant to
XPath syntax.

Thanks.

Sep 25 '06 #1
8 12927
Ah, I just hacked out that XML, just imagine it is properly formed for
it's purpose.

Sep 25 '06 #2
se*****@gmail.c om wrote:
How would I use XPath to select an element where the text equals what
I'm looking for?
You use a predicate.
books with an element named "topic" text of "C++" what would the xpath
be?
I presume you meant to have a "book" element that groups the values that
are properties of the same book:

<books>
<category>
<book>
<topic>Linux</topic>
<title>Linux ABC's</title>
<desc>The ABC's of Linux</desc>
</book>
<book>
<topic>C++</topic>
<title>Beginnin g C++</title>
<desc>C++ programming for beginners</desc>
</book>
<book>
<topic>C++</topic>
<title>Patter ns in C++</title>
<desc>Advance d C++ programming</desc>
</book>
</category>
</books>

You could then say, for example, /books/category/book[topic="C++"]

I thought it would be something like //books/category[topic='C++']
That should work on your sample document (see above grumble about the
design). But what it will find is the category element, not an
individual book.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Sep 25 '06 #3

Joseph Kesselman wrote:
se*****@gmail.c om wrote:
How would I use XPath to select an element where the text equals what
I'm looking for?

You use a predicate.
books with an element named "topic" text of "C++" what would the xpath
be?

I presume you meant to have a "book" element that groups the values that
are properties of the same book:

<books>
<category>
<book>
<topic>Linux</topic>
<title>Linux ABC's</title>
<desc>The ABC's of Linux</desc>
</book>
<book>
<topic>C++</topic>
<title>Beginnin g C++</title>
<desc>C++ programming for beginners</desc>
</book>
<book>
<topic>C++</topic>
<title>Patter ns in C++</title>
<desc>Advance d C++ programming</desc>
</book>
</category>
</books>

You could then say, for example, /books/category/book[topic="C++"]

I thought it would be something like //books/category[topic='C++']

That should work on your sample document (see above grumble about the
design). But what it will find is the category element, not an
individual book.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Thanks, I just added another element and it worked fine.

Second, how can I use select-distinct so I only return one category
topic? I see how to do it with attributes selected but I'm not having
a lot of immediate success trying to get it to work just on elements.

Sep 26 '06 #4
se*****@gmail.c om wrote:
Second, how can I use select-distinct so I only return one category
topic?
I'm not sure what you mean by "select-distinct". The answer is probably
still predicates, but please provide an example of what you're trying to
do. Mindreading, even in XML, is tough.

(Standard pointer: http://www.catb.org/~esr/faqs/smart-questions.html)

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Sep 26 '06 #5
se*****@gmail.c om wrote:
Hello,

I've been reading up on xpath and I am able to access elements with it.
I haven't been able to figure one thing out though.

How would I use XPath to select an element where the text equals what
I'm looking for?
Let's assume you have some well-formed XML:

<books>
<category class="computin g">
<topic subject="Linux" >
<book>
<title>Linux ABC's</title>
<desc>The ABC's of Linux</desc>
</book>
</topic>
<topic subject="C++">
<book>
<title>Beginnin g C++</title>
<desc>C++ programming for beginners</desc>
</book>
<book>
<title>Patter ns in C++</title>
<desc>Advance d C++ programming</desc>
</book>
</topic>
</category>
</books>

(Far from ideal, but usable)

Then books about C++ can be got by several means:

//topic[@subject='C++']/book
//book[contains(title, 'C++')]
//book[contains(desc,' C++')]

or some combination of them.
I thought it would be something like //books/category[topic='C++'] but
I get a node test expected error.
Hardly surprising if your XML is not well-formed. Get the data model
right first, then everything else pretty much falls into place. Get it
wrong, and your application is hosed before you start.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Sep 26 '06 #6
And be careful if your data is coming from someone who doesn't
understand XML. I see a lot of data these days being generated by
scripts being careless about newlines, eg

<desc>
C++
</desc>

and of course /foo[desc='C++'] fails because of the white-space.
contains() is your friend.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Sep 26 '06 #7
Great info here! You're saving me a lot of trial and error.

The select-distinct would be like a select distrinct SQL statement.

Let's say I have 200 records to search and I only want to know all the
topic categories out there. Rather than display a result that might
show the category C++ 34 times, I want to return C++ once. Basically
my end result is every category available but only displaying it once.

C++
C++
LINUX
C++
JAVA
PHP
PHP
PERL
C++
..NET

Selecting distrinct would return. I've seen some XPATH function like
this, but they only work with attributes, I would like to use the
element name.

C++
LINUX
JAVA
PHP
PERL
..NET

~Scott

Sep 28 '06 #8

se*****@gmail.c om wrote:
Great info here! You're saving me a lot of trial and
error.
Trial and error is an excellent way to learn, as long as
you're not coding a targetting software package for ICBMs.
Let's say I have 200 records to search and I only want to
know all the topic categories out there.
Rather than display a result that might show the category
C++ 34 times, I want to return C++ once. Basically my end
result is every category available but only displaying it
once.
You seem to understand pretty well what exactly do you
need, you should've tried implementing it.
I've seen some XPATH function like this, but they only
work with attributes, I would like to use the element
name.
What does it matter? So it's in child:: axis instead of
attribute:: axis. Big deal.

Assume you have your data stored in an XML file like this:

<?xml version="1.0" encoding="UTF-8"?>
<data>
<book><topic>Pe rl</topic></book>
<book><topic>C+ +</topic></book>
<book><topic>Ja va</topic></book>
<book><topic>C+ +</topic></book>
<book><topic>Pe rl</topic></book>
<book><topic>Ja va</topic></book>
<book><topic>Ja va</topic></book>
<book><topic>C+ +</topic></book>
<book><topic>Ja va</topic></book>
<book><topic>Pe rl</topic></book>
</data>

(Nodes that we aren't using in this example omitted.)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:styleshe et
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
version="1.0"
>
<xsl:output
method="xml"
version="1.0"
encoding="UTF-8"
/>
<xsl:template match="/">
<result>
<xsl:apply-templates
select="//book"
mode="distinct-topic"
>
<xsl:sort select="topic"/>
</xsl:apply-templates>
</result>
</xsl:template>
<xsl:template match="book" mode="distinct-topic">
<!-- only if the current node is the first node in the -->
<!-- document with the same content of <topicchild -->
<xsl:if
test="
generate-id(.)=
generate-id(//book[topic=current()/topic][1])
"
>
<xsl:copy-of select="topic"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

--
Pavel Lepin

Sep 29 '06 #9

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

Similar topics

2
5048
by: ahogue at theory dot lcs dot mit dot edu | last post by:
Hello - Is there any way to match complex subtree patterns with XPath? The functions I see all seem to match along a single path from root to leaf. I would like to match full subtrees. For example, given the XHTML: <html> <body>
5
2528
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element of the currently matched element in the template, so that XPath expressions inside would be relative to that node? Specifically, I want to do this, in order to be able to issue xsl:call-template to apply some templates to some
3
5062
by: Johannes Koch | last post by:
Hi there, I'd like to apply an xpath to both HTML and XHTML documents. First I create a DOM document with a Java DOM parser, then apply the xpath with Xalan's XPathAPI class. The problem is that in HTML DOM element names are all upper-case, whereas in Core DOM (used for the XHTML documents) element names are lower-case. When I use a lower-case xpath, e.g. /head
3
3041
by: Tjerk Wolterink | last post by:
Hello i have xml code like this: <page:page xmlns:page="namespacefor page"> <page:section> <page:header> <b>Hello</b>There </page:header> <page:content> --- HTML CODE like: <i>Y</i>es i like bla bal <center>bla</center><img> alblalba
9
16641
by: Tjerk Wolterink | last post by:
Hello, suppose i have a dom like this: <a> - <b> - <b> - <d> - </b> - <c>
4
3446
by: Pat Turner | last post by:
Hi, I have some XML like this: <family> <person name="bob"> <father ref="../../person" /> </person> <person name="charlie"> <child ref="../../person" />
4
21202
by: Son KwonNam | last post by:
In XSLT, is this possible to get value from xml using XPath which is in XSLT variable? I mean XPath strings can be dynamic while XSL Transforming. If possible, How?? Because I'm not a native English speaker, it's quite hard to make the problem clear. Please see the following example.
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8341
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8539
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.