accessing imported XML element attribute with a gi

2019-09-17 14:55发布

问题:

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.

回答1:

Answer thanks to LarsH

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