tags:

views:

36

answers:

1

I am trying to parse the following XML using JSTL. I can easily retrieve all the elements in the XML apart from the tags with the "t" prefix, how do I do this?

XML looks like (feed.xml)

<rss version="2.0" xmlns:t="http://www.xxx.co.uk/xxx"&gt;
<channel>
    <title></title>
    <link></link>
    <description></description>
    <language>en-uk</language>
    <copyright></copyright>
    <webMaster></webMaster>
    <pubDate>Thu, 02 Sep 2010 16:56:49 +0100</pubDate>
    <lastBuildDate>Thu, 02 Sep 2010 17:24:03 +0100</lastBuildDate>
    <ttl></ttl>

    <image>
        <title></title>
        <link></link>
        <width></width>
        <height></height>
        <url></url>
    </image>

    <category></category>

    <item>
        <title>Title text 1</title>
        <link>http://a.b.com/link1&lt;/link&gt;
        <description>Description text 1</description>
        <enclosure url="http://a.b.com/xxxx/yyyy1.jpg" length="999" type="image/jpeg" />
        <guid>Unique-identifier-1</guid>
        <test>
            <test1>azman</test1>
        </test>
        <t:fields>
            <t:isfeatured>true</t:isfeatured>
        </t:fields>
    </item>
</channel>

and my JSTL looks like this

<c:import url="feed.xml" var="xml"/><x:parse xml="${xml}" var="doc"/>

The following will return "Title text 1"

<x:out select="$doc/rss/channel/item/title" />

However the following will cause the following error

<x:out select="$doc/rss/channel/item/t:fields/t:isfeatured" />

Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:401) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) root cause

javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Error evaluating XPath expression "/rss/channel/item/t:fields/t:isfeatured": javax.xml.transform.TransformerException: Prefix must resolve to a namespace: t org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:858) org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791) org.apache.jsp.index_jsp._jspService(index_jsp.java:106) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) root cause

javax.xml.transform.TransformerException: Prefix must resolve to a namespace: t org.apache.xpath.compiler.XPathParser.error(XPathParser.java:640) org.apache.xpath.compiler.Lexer.mapNSTokens(Lexer.java:676) org.apache.xpath.compiler.Lexer.tokenize(Lexer.java:303) org.apache.xpath.compiler.Lexer.tokenize(Lexer.java:134) org.apache.xpath.compiler.XPathParser.initXPath(XPathParser.java:146) org.apache.xpath.XPath.(XPath.java:200) org.apache.taglibs.standard.tag.common.xml.JSTLXPathAPI.eval(JSTLXPathAPI.java:285) org.apache.taglibs.standard.tag.common.xml.XPathUtil.valueOf(XPathUtil.java:472) org.apache.taglibs.standard.tag.common.xml.ExprSupport.doStartTag(ExprSupport.java:64) org.apache.jsp.index_jsp._jspx_meth_x_005fout_005f0(index_jsp.java:173) org.apache.jsp.index_jsp._jspService(index_jsp.java:91) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

Thanks for any help!

A: 

It appears that while your document specifies a namespace declaration, the XPath engine being invoked from the JSTL code is not aware of the namespace, which for Xalan is provided in a Namespace Context. I don't know JSTL, but you might investigate how to set a namespace context for JSTL.

I googled for jstl xpath namespace context and got one interesting hit, which unfortunately does not contain a solution.

EDIT: Looks like you can do it in JSTL 1.0 only with the namespace-uri() function, leading to long, hard-to-read XPath expressions. See here

Jim Garrison
Thanks for your help.I did try using the namespace-uri() function but again nothing was returned <x:out select="$doc/rss/channel/item/t:fields[namespace-uri() = 'http://www.xxx.com/xxx']/t:isfeatured[namespace-uri() = 'http://www.xxx.com/xxx']" />
A Edwards