嘿那里,
我有一个像这样的xml文档:
<Movies>
<Movie>
<Title>AAAA</Title>
<Description>aaaa</Description>
</Movie>
<Movie>
<Title>BBBB</Title>
<Description>bbbb</Description>
</Movie>
</Movies>
我希望使用XPath迭代本文档的元素并返回标题和描述,问题是我设法只返回其中一个。这是我的代码:
path="/Movies/Movie/Title";
var nodes=xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
var result=nodes.iterateNext();
while (result)
{
document.write('<figure class="effect-oscar wowload fadeInUp">');
document.write('<img src="images/portfolio/1.jpg" alt="img01"/>');
document.write('<figcaption>');
document.write('<h2>');
document.write(result.childNodes[0].nodeValue);
document.write('</h2>');
document.write('<p>Detalii despre film 1<br>');
document.write('<a href="images/portfolio/1.jpg" title="1" data-gallery>View more</a></p>');
document.write('</figcaption>');
document.write('</figure>');
result=nodes.iterateNext();
}
}
直到现在我设法归还了这个头衔。如何在同一个while循环中返回Description?
稍后编辑:我使用这段代码解决了我的问题:
xmlDoc=loadXMLDoc("movies1.xml");
for(i=0; i<12; i++){
document.write('<figure class="effect-oscar wowload fadeInUp">');
pic1 = '<img src="';
pic2 = '" alt="img01"/>';
c=xmlDoc.getElementsByTagName("Picture")[i]
d=c.childNodes[0];
pic3=pic1.concat(d.nodeValue);
pic4=pic3.concat(pic2);
document.write(pic4);
document.write('<figcaption>');
document.write('<h2>');
x=xmlDoc.getElementsByTagName("Title")[i]
y=x.childNodes[0];
document.write(y.nodeValue);
document.write('</h2>');
document.write('<p>');
a=xmlDoc.getElementsByTagName("Description")[i]
b=a.childNodes[0];
document.write(b.nodeValue);
document.write('<br>');
document.write('<a href="https://www.google.ro" title="1">View trailer</a></p>');
//document.write('<a class="youtube" href="http://www.youtube.com/watch?v=4eYSpIz2FjU" title="jQuery YouTube Popup Player Plugin TEST">Test Me</a></p>')
document.write('</figcaption>');
document.write('</figure>');
}
谢谢!
答案 0 :(得分:1)
请勿对/Movies/Movie/Title
进行迭代,仅对/Movies/Movie
进行迭代,并在每个步骤中检索Title
和Description
(或./Title
和{ {1}},各自)。