Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从XPath javascript循环中单独设置xml元素的样式?_Javascript_Xml_Loops_Xpath - Fatal编程技术网

如何从XPath javascript循环中单独设置xml元素的样式?

如何从XPath javascript循环中单独设置xml元素的样式?,javascript,xml,loops,xpath,Javascript,Xml,Loops,Xpath,因此,我尝试使用xpath(html页面中的javascript)从xml文件中提取所选数据。我遇到的唯一问题是:找到一种方法来分别设置每个值的样式,而不是(当前)一次只能设置整个流的样式 我假设这只是一个理解javascript循环的问题,但如果您能提供任何快速帮助,我将不胜感激。谢谢 xml文件基本上是这样的: 下面是XPath/js代码: <script type="text/javascript"> function loadXMLDoc(dname) { if (windo

因此,我尝试使用xpath(html页面中的javascript)从xml文件中提取所选数据。我遇到的唯一问题是:找到一种方法来分别设置每个值的样式,而不是(当前)一次只能设置整个流的样式

我假设这只是一个理解javascript循环的问题,但如果您能提供任何快速帮助,我将不胜感激。谢谢

xml文件基本上是这样的:

下面是XPath/js代码:

<script type="text/javascript">
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}

xml=loadXMLDoc("catalog.xml");
path="/catalog/cd[year=1985]/title | /catalog/cd[year=1985]/company | /catalog/cd[year=1985]/country";

// code for IE
if (window.ActiveXObject)
{
var nodes=xml.selectNodes(path);

for (i=0;i<nodes.length;i++)
  {
  document.write("<font color=\"red\">");
  document.write(nodes[i].childNodes[0].nodeValue);
  document.write("</font>");
  document.write("<hr>");
  }
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
var nodes=xml.evaluate(path, xml, null, XPathResult.ANY_TYPE,null);
var result=nodes.iterateNext();

while (result)
  {
  document.write("<strong>");
  document.write(result.childNodes[0].nodeValue);
  document.write("</strong>");
  document.write("<br />");
  result=nodes.iterateNext();
  }
}
</script>

函数loadXMLDoc(dname)
{
if(window.XMLHttpRequest)
{
xhttp=newXMLHttpRequest();
}
其他的
{
xhttp=新的ActiveXObject(“Microsoft.XMLHTTP”);
}
xhttp.open(“GET”、dname、false);
xhttp.send(“”);
返回xhttp.responseXML;
}
xml=loadXMLDoc(“catalog.xml”);
path=“/catalog/cd[year=1985]/title |/catalog/cd[year=1985]/company |/catalog/cd[year=1985]/country”;
//IE代码
if(window.ActiveXObject)
{
var nodes=xml.selectNodes(路径);

对于(i=0;iI不清楚“值”和“流”是什么意思,你能详细说明你想要实现什么吗?当然,我会尝试。这三条路径中的每一条都会输出到页面,如下所示:“标题A,公司A,国家A,标题B,公司B,国家B”例如,我可以将整个输出设置为红色,但我需要做的是将每个标题设置为红色,将每个公司设置为绿色(例如)。还可以在每个“集合”(标题、公司、国家)之间插入html标记(如br或hr)如果阅读清晰就好了。希望能更清晰一点?是的,这是预期的输出,管道符号的意思是“匹配其中任何一个”XPath一个接一个地遍历每个元素。这段代码有几处错误。首先,
Microsoft.XMLHTTP
已经过时,不要再使用它了。有
MSXML2.XMLHTTPRequest
来替换它。接下来,不要执行同步请求,而是使用onreadystatechange事件和回调函数。接下来,不要(!!)执行
document.write()
。哦,接下来,您没有使用
var
关键字来声明变量,但无论如何您应该这样做。好的,Viruzzo,所以我的问题是如何在“管道”之间设置样式,而不是将所有管道作为一个组来设置样式?