Doh! I wasn't paying close enough attention to your newsSources XML. I
would think it should work like this:
<x:forEach select="$newsSources//eachNewsSource[sourceID=$source]">
Because sourceID isn't an attribute of eachNewsSource, it's a child of
it. You only use the '@' sign for referencing an attribute. If it was
an attribute, the XML would look like:
<allNewsSources>
<eachNewsSource sourceID="am">
<sourceTitle>Apparel Magazine</sourceTitle>
<sourceUrl>http://www.apparelmag.com/</sourceUrl>
</eachNewsSource>
</allNewsSources>
In that case, what you had before should work:
<x:forEach select="$newsSources//[EMAIL PROTECTED]">
But, if you don't want to change your XML, then I would remove the '@/'
from in front of sourceID to alleviate confusion for the next guy. :)
Chris
-----Original Message-----
From: Dylan MacDonald [mailto:[EMAIL PROTECTED]
Sent: Monday, March 29, 2004 6:43 PM
To: [EMAIL PROTECTED]
Cc: 'Bill Siggelkow'; Johnson, Chris
Subject: RE: XML XPath expression help
Well, the brute force technique sometimes works. After a million
different combinations, I got the XPath expression working by changing
one thing in the expression below. Can you guess what it is? A leading
slash on the sourceID attribute:
<x:forEach select="$newsSources//eachNewsSource[@/sourceID=$source]">
Thanks for the help and your time.
Dylan
-----Original Message-----
From: Bill Siggelkow [mailto:[EMAIL PROTECTED]
Sent: Friday, March 26, 2004 12:42 PM
To: [EMAIL PROTECTED]
Subject: Re: XML XPath expression help
Dylan, you could try using <x:set> to expose the sourceID as a variable
that then referenced in the inner <x:forEach> like the following:
<x:forEach select="$news//eachNewsArticle">
<x:set select="sourceID" var="source"/>
<tr>
<td>
<x:out select="articleDate" />
</td>
<x:forEach
select="$newsSources//[EMAIL PROTECTED]">
...
It should work but I have not actually tried it so YMMV ...
Dylan MacDonald wrote:
> Hi -
>
> Last week I had a question about writing an XPath expression that
> would retrieve only the first few records from an XML source. I got
> an immediate response which worked perfectly. Hopefully I'll have the
> same luck with this question.
>
> I have a JSP page that displays a list of random internet articles
> that I use in my corporate intranet. The list includes the article
> title and a link to the article, as well as the source and the link to
> the source's website.
>
> While the articles are all unique, the sources are not. Rather than
> duplicate the name and URL of each source for each article, I would
> create a second XML file for the sources, assign a unique ID to each
> source and then reference that ID in my articles XML file. Then, on
> the JSP page itself, I would import both XML files and then just use
> some kind of XPath expression in the forEach statement to compare the
> IDs. Well, this doesn't work, at least the way I did it. It just
> gives me an error.
>
> I'm sure my code has many problems, but I think the two main ones are:
> can you even import two separate XML files with JSTL; and secondly, my
> XPath expression is probably wrong. Anyway if anyone has any clues,
> I'd surely appreciate it.
>
>
> Below is my sample code:
>
> news.xml
>
> <allNewsArticles>
> <eachNewsArticle>
> <articleDate>October 31, 2003</articleDate>
> <articleTitle>More quality along the supply
chain</articleTitle>
>
> <articleUrl>http://english.lz-net.de/news/webtechnews/pages/showmsg.pr
> l?id=3
> 097</articleUrl>
> <sourceID>lz</sourceID>
> </eachNewsArticle>
> </allNewsArticles>
>
> ####
>
> newsSources.xml
>
> <allNewsSources>
> <eachNewsSource>
> <sourceID>am</sourceID>
> <sourceTitle>Apparel Magazine</sourceTitle>
> <sourceUrl>http://www.apparelmag.com/</sourceUrl>
> </eachNewsSource>
> </allNewsSources>
>
>
> ####
>
> articles.jsp
>
> <c:import var="news_xml" url="news.xml" />
> <c:import var="newsSources_xml" url="newsSources.xml" /> <x:parse
> var="news" xml="${news_xml}" /> <x:parse var="newsSources"
> xml="${newsSources_xml}" />
>
> --snip--
>
> <x:forEach select="$news//eachNewsArticle">
> <tr>
> <td>
> <x:out select="articleDate" />
> </td>
> <x:forEach select="$newsSources//eachNewsSource[sourceID =
> '$news//eachNewsArticle/sourceID']">
> <td>
> <a href="<x:out select='sourceUrl' />"><x:out
select="sourceTitle" /></a>
> </td>
> </x:forEach>
> <td>
> <a href="<x:out select='articleUrl' />"><x:out
> select="articleTitle" /></a>
> </td>
> </tr>
> </x:forEach>
>
>
>
> Dylan MacDonald
> Senior Web Designer
> GNX
> phone: 415-283-3715
> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]