JSTL - 解析不适​​用于具有命名空间的元素

时间:2010-10-10 00:48:57

标签: xml parsing jstl

我正在尝试使用JSTL解析以下XML。除了带有“t”前缀的标签之外,我可以轻松检索XML中的所有元素,我该怎么做?

XML看起来像(feed.xml)

<rss version="2.0" xmlns:t="http://www.xxx.co.uk/xxx">
<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</link>
        <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>

我的JSTL看起来像这样

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

以下内容将返回“标题文字1”

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

但是以下情况会导致以下错误

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

堆栈跟踪:     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) 根本原因

javax.servlet.ServletException:javax.servlet.jsp.JspTagException:评估XPath表达式时出错“/ rss / channel / item / t:fields / t:isfeatured”:javax.xml.transform.TransformerException:前缀必须解析为命名空间: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) 根本原因

javax.xml.transform.TransformerException:前缀必须解析为命名空间: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)

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

看来,当您的文档指定名称空间声明时,从JSTL代码调用的XPath引擎不知道名称空间,而Xalan是在命名空间上下文中提供的。我不知道JSTL,但您可能会研究如何为JSTL设置名称空间上下文。

我用Google搜索jstl xpath namespace context并得到一个interesting hit,但遗憾的是,它不包含解决方案。

编辑:看起来你只能使用namespace-uri()函数在JSTL 1.0中执行此操作,从而导致长的,难以阅读的XPath表达式。见here