当标签值与使用 JavaScript 的输入匹配时,返回 XML 标签属性

Return XML tag attribute when tag value matches input using JavaScript

本文关键字:标签 XML 属性 返回 JavaScript 输入      更新时间:2023-09-26

我正在使用下面显示的XML代码。 我已经请求并检索了代码。 我需要编写一个函数,它将: 1) 接收输入的代码 2)动态返回数据库中与subnational1-code="US-MI"和标签值"Kent"对应的次国家2代码

<result>
  <location country-code="US" subnational1-code="US-TX" subnational2-code="US-TX-263">Kent</location>
  <location country-code="US" subnational1-code="US-VA" subnational2-code="US-VA-127">New Kent</location>
  <location country-code="US" subnational1-code="US-DE" subnational2-code="US-DE-001">Kent</location>
  <location country-code="US" subnational1-code="US-KY" subnational2-code="US-KY-117">Kenton</location>
  <location country-code="US" subnational1-code="US-MD" subnational2-code="US-MD-029">Kent</location>
  <location country-code="US" subnational1-code="US-MI" subnational2-code="US-MI-081">Kent</location>
  <location country-code="US" subnational1-code="US-RI" subnational2-code="US-RI-003">Kent</location>
  <location country-code="CA" subnational1-code="CA-NB" subnational2-code="CA-NB-KE">Kent</location>
  <location country-code="CA" subnational1-code="CA-ON" subnational2-code="CA-ON-KT">Chatham-Kent</location>
  <location country-code="GB" subnational1-code="GB-ENG" subnational2-code="GB-ENG-KEN">Kent</location>
</result>

XML 代码网址。

以下是我过去使用过的代码(getData 工作),或者正在尝试找出。 这不一定必须使用:

var subnational1-code="US-MI";
var subnational2-name="Kent";
var itemList = response.getElementsByTagName("result");
for(i=0;i<itemList.length){
  var d = getData(itemList.item(i));
  //  What code goes here??
  //  If (subnational2-value==subnational2-name&&subnational1-valuue=="US-MI");
}
function getData(n) {
  var d = new Object();
  var nodeList = n.childNodes;
  for (var j = 0; j < nodeList.length ; j++) {
  var node = nodeList.item(j);
  d[node.nodeName] = node.firstChild.nodeValue;
  } return d;
}

在这种情况下,输出应为:

 var output = "US-MI-081";

我非常感谢您的帮助。 +1 和更多任何可以为我提供工作功能的人! 提前感谢!!

不确定这是否完全是您要查找的内容,但这里有一种 javascript 方法来解析它。 希望这对您有用。

var output = "";
var subnational1Code="US-MI";
var subnational2Name="Kent";
var itemList = document.getElementsByTagName("result");
for(var i = 0;i<itemList.length; i++){
  var d = getData(itemList.item(i));
  for(var k in d)
    if (d[k].sub1 === subnational1Code && d[k].value === subnational2Name)
      output = d[k].sub2;
}
function getData(n) {
  var objArray = new Array();
  var nodeList = n.childNodes;
  for (var j = 0; j < nodeList.length ; j++) {
  var node = nodeList.item(j);
  var o = new Object();
  o["value"] = node.firstChild.nodeValue;
  o["country"] = node.attributes["country-code"].nodeValue;
  o["sub1"] = node.attributes["subnational1-code"].nodeValue;
  o["sub2"] = node.attributes["subnational2-code"].nodeValue;
  objArray.push(o);
  } return objArray;
}
// console.log(output); //in case you wanted to debug it

我会使用 XPath 来检索节点。

尝试使用 document.evaluate() 函数:

var xpathResult = document.evaluate("location[@subnational1-code='US-MI' and text() = 'Kent']/@subnational2-code", itemList, null, XPathResult.ANY_TYPE, null);  
相关文章: