Dylan,

Instead of imbedding the second xpath expression inside the other, try
doing an x:set for the sourceID right at the beginning of the first
forEach (eachNewsArticle), then just refer to the variable in the
subsequent forEach's select attribute.

If this doesn't help, I'm sure Kris will be along shortly with a much
more thoroughly baked answer. :):)

Chris

-----Original Message-----
From: Dylan MacDonald [mailto:[EMAIL PROTECTED] 
Sent: Friday, March 26, 2004 2:23 PM
To: [EMAIL PROTECTED]
Subject: XML XPath expression help


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.prl?
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to