Hi John
From: "John Townsend" <[EMAIL PROTECTED]>
> I am having a problem with forEach in the xtags library. I am trying to
> use XPath and xtags to select all of the person entries from my
> directory that contain a substring (For example, all of the people who
> have 'John' in their name).
>
> I have an XML file that looks basically like this:
>
> <directory>
> <person>
> <name>John Townsend</name>
> ...more data...
> </person>
> </directory>
>
> Here's a piece of my JSP page:
>
> <xtags:forEach select = "//person[child::name[contains(text(),
> '$param')]]">
The $param should not be in quotes. Putting that in quotes means look for a
name that contains the literal string "$param" such as
<directory>
<person>
<name>Foo $param Townsend</name>
...more data...
</person>
</directory>
So the XPath expression to evaluate the current param variable that you need
is
//person[contains(name,$param)]
I took the liberty of simplifying the XPath syntax a bit. More on that
below...
> If my select in the forEach looks like this:
>
> Select = "//person[child::name[contains(text(), 'John')]]"
>
> Then it works.
BTW you can drop the child:: axis as that is the default axis to
//person[name[contains(text(), 'John')]]
Then the contains() function turns both objects into strings so you could
simplify that again to
//person[name[contains(., 'John')]]
which is the same as
//person[contains(name, 'John')]
Remember only use '' to denote literal strings, never use it for names of
elements, attributes or variables.
> Forgive my ignorance of XPath, I am just starting to use the
> xtags library and XPath.
No problem.
I can highly recommend the Zvon tutorial for XPath, it takes about 10-20
minutes and teaches at least 90% of XPath thats important using some simple
examples. (I always find learning by example easier than reading specs,
particularly for languages)
http://www.zvon.org/xxl/XPathTutorial/General/examples.html
James
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com