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

标签 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"/>

下面会返回“Title text 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)

感谢您的帮助!

最佳答案

看起来虽然您的文档指定了 namespace 声明,但从 JSTL 代码调用的 XPath 引擎并不知道 namespace ,对于 Xalan, namespace 上下文中提供了 namespace 。我不了解 JSTL,但您可能会研究如何为 JSTL 设置命名空间上下文。

我用谷歌搜索了 jSTL xpath namespace context 并得到了一个 interesting hit ,不幸的是,它不包含解决方案。

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

关于xml - JSTL - 解析不适​​用于具有命名空间的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3898512/

相关文章:

java - 在jsp中使用JSTL标签

java - 从 JSP 调用 Tomcat 7 方法

c# - XPath 从当前元素查找动态父/祖先元素?

c# - 当密码或用户名字段包含特殊字符时,System.Uri 无法解析

json - 我需要使用 Alamofire 解析 Json 的帮助

javascript - 在 JavaScript 中解析带有转义双引号属性的 JSON 对象

java - Spring MVC JSTL预选列表

xml - 揭秘 XPATH

php - php xpath查询以基于重复子节点中的值获取父节点

xml - EDOM 解析错误(在文本中发现无效字符)/韩文字符问题