在JSP中使用给定的命名空间访问导入的XML元素属性 [英] accessing imported XML element attribute with a given namespace in a JSP

查看:285
本文介绍了在JSP中使用给定的命名空间访问导入的XML元素属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在这里做了我的研究,但是找不到合适的答案。我不知道我做错了什么我知道有类似的问题,但不完全是我的问题。



我正在jstl中导入rss feed:

  < c:import url =http:// urltoRSSfeedvar =rssFeed/> 

一旦我有资料,我正在使用DocumentBuilderFactory构建XML文档

 <%String xml =(String)pageContext.getAttribute(rssFeed); 
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
文档doc = db.parse(is);
pageContext.setAttribute(newXML,doc);%>

这不是问题。在运行该scriptlet之后,我可以访问XML文档中的几乎所有元素。问题是在这个XML文档中有一个元素正在使用我无法访问的声明的名称空间。



rss的顶部看起来像这样



 < rss xmlns:media =http://namespaceUrl.com/foo/version =2.0> 

我想要访问的元素属性看起来像这样

 < channel> 
< item>
< title>我可以访问这个罚款< / title>
< description>我可以访问这个罚款< / description>
<! - 但是,无法访问此URL属性 - >
< media:content url =http://www.IwantThisUrl.com/>
< / item>
< / channel>

现在,我已经对许多不同的表达式做了一些测试,但是我无法让它工作。最近访问url属性的尝试是这样的:

 < x:set var =urlselect =string ($ newXML / channel / item / * [local-name()='content'和namespace-uri()='http://namespaceUrl.com/foo/'] / @ url)/> 

这不返回预期的值:' http://namespaceUrl.com/foo/ '



任何人都对我所做的任何建议错误?感谢提前。

解决方案

回答谢谢LarsH

  string($ newXML / channel / item / * / @ url)


So I have done my research on here but was not able to find a suitable answer. I am not sure what I am doing wrong. I know there have been similar questions but not quite exactly my issue.

I am importing an rss feed in jstl like so:

<c:import url="http://urltoRSSfeed" var="rssFeed" />

Once I have the feed, I am building the XML doc using DocumentBuilderFactory

<%  String xml = (String)pageContext.getAttribute("rssFeed");
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xml));
    Document doc = db.parse(is);
    pageContext.setAttribute("newXML",doc);%>

This is not the issue. I can access almost every element in the XML doc after this scriptlet has run. The issue is that within this XML doc there is an element that has is using a declared name space that I cannot access.

The top of the rss looks like this

<rss xmlns:media="http://namespaceUrl.com/foo/" version="2.0">

And the elements attribute that I am trying to access looks like this

<channel>
     <item>
          <title>I can access this fine</title>
          <description>I can access this fine</description>
          <!-- however, cannot access this url attribute below-->
          <media:content url="http://www.IwantThisUrl.com" /> 
     </item>
</channel>

Now, I have done some testing on many different expressions but I can't get it to work. The most recent attempt at accessing the url attribute was this :

<x:set var="url" select="string($newXML/channel/item/*[local-name()='content' and namespace-uri()='http://namespaceUrl.com/foo/']/@url)" />

This does not return the expected value of : 'http://namespaceUrl.com/foo/'

Anyone have any suggestions on what I am doing wrong? Thanks in advance.

解决方案

Answer thanks to LarsH

string($newXML/channel/item/*/@url)

这篇关于在JSP中使用给定的命名空间访问导入的XML元素属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆