You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by Dylan MacDonald <dm...@gnx.com> on 2004/03/26 21:23:24 UTC

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: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


RE: XML XPath expression help

Posted by Dylan MacDonald <dm...@gnx.com>.
Thanks to both users who gave me pointers on this.  It's gotten me so close,
but I'm not quite there yet.

Setting the "sourceID" ID to the variable "source" every iteration is a good
idea and works fine (I added <x:out select="$source" /> within the outer
forEach to verify).  However, the XPath expression that compares this
variable with the sourceID from the news source XML file doesn't seem to
work:

<x:forEach select="$newsSources//eachNewsSource[@sourceID=$source]">

It doesn't error out; it just doesn't give any results.  Any other thoughts?

Thanks again for the time,

Dylan



-----Original Message-----
From: Bill Siggelkow [mailto:billsigg@bellsouth.net]
Sent: Friday, March 26, 2004 12:42 PM
To: taglibs-user@jakarta.apache.org
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//eachNewsSource[@sourceID=$source]">
...

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.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: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


RE: XML XPath expression help

Posted by Dylan MacDonald <dm...@gnx.com>.
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:billsigg@bellsouth.net]
Sent: Friday, March 26, 2004 12:42 PM
To: taglibs-user@jakarta.apache.org
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//eachNewsSource[@sourceID=$source]">
...

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.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: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: XML XPath expression help

Posted by Bill Siggelkow <bi...@bellsouth.net>.
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//eachNewsSource[@sourceID=$source]">
...

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.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: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org