首页 > 解决方案 > Javascript:解析 xml 以在节点内获取匹配的标记值

问题描述

我是初学者级别的javascripter,并且有以下问题。我正在将 javascript 功能添加到评估表中。

用户将从下拉列表中选择一个项目,给我一个值,我可以在变量中获取一个东西。(例如, var 食欲 = 2)。

然后,我将一个 XML 文档作为变量加载到 DOM 中。我需要做的是在 XML 中搜索匹配节点中的值并获取配对值给我一个分数。

我的 XML 看起来像这样:-

 <root Patient_ID="1333" Current_User="Iain Cowden" Current_Date="05/02/2021" Current_Time="20:15">
  <SQLQuery elementNameInResults="BMI" recordCount="1">select top 1 BMI ,<br/>case when BMI between 20 and 24.9 then 1<br/>when BMI between 25 and 29.9 then 2<br/>when BMI >=30 then 3<br/>when BMI<20 then 0 end as BMI_Score,<br/>case when gv.Gender_ID=2 then 2 <br/>when gv. Gender_ID=3 then 1 end as Gender_Score,<br/>case when dbo.GetAge(Date_Of_Birth,Estimated_Year_Of_Birth,Date_Of_Death) <=49 then 1<br/>when dbo.GetAge(Date_Of_Birth,Estimated_Year_Of_Birth,Date_Of_Death) between 50 and 64 then 2<br/>when dbo.GetAge(Date_Of_Birth,Estimated_Year_Of_Birth,Date_Of_Death) between 65 and 74 then 3<br/>when dbo.GetAge(Date_Of_Birth,Estimated_Year_Of_Birth,Date_Of_Death) between 75 and 80 then 4<br/>when dbo.GetAge(Date_Of_Birth,Estimated_Year_Of_Birth,Date_Of_Death) >80 then 5 end as Age_Score,<br/>dbo.GetAge(Date_Of_Birth,Estimated_Year_Of_Birth,Date_Of_Death) as Age,<br/>gv.Gender_Desc as Gender<br/><br/>from tblCHTHealthNoteMeasure nm<br/>join tblpatient p on p.Patient_ID=nm.Patient_ID<br/>join tblgendervalues gv on p.gender_id=gv.gender_id<br/>where nm.Patient_ID=@patientID order by Health_Note_Date desc<SqlParameter name="@patientID" value="1333" type=""></SqlParameter></SQLQuery>
  <BMI>
    <BMI>35.2</BMI>
    <BMI_Score>3</BMI_Score>
    <Gender_Score>1</Gender_Score>
    <Age_Score>4</Age_Score>
    <Age>80</Age>
    <Gender>Male</Gender>
  </BMI>
  <SQLQuery elementNameInResults="WLAppetiteID" recordCount="4">select UDP_WLAppetite_ID as WLAppetiteID, External_Code1 as WLAppetite from udpWLAppetiteValues where @patientID=@patientID<SqlParameter name="@patientID" value="1333" type=""></SqlParameter></SQLQuery>
  <WLAppetiteID>
    <WLAppetiteID>0</WLAppetiteID>
    <WLAppetite>0</WLAppetite>
  </WLAppetiteID>
  <WLAppetiteID>
    <WLAppetiteID>1</WLAppetiteID>
    <WLAppetite>2</WLAppetite>
  </WLAppetiteID>
  <WLAppetiteID>
    <WLAppetiteID>2</WLAppetiteID>
    <WLAppetite>3</WLAppetite>
  </WLAppetiteID>
  <WLAppetiteID>
    <WLAppetiteID>3</WLAppetiteID>
    <WLAppetite>5</WLAppetite>
  </WLAppetiteID>

例如,我知道如何使用 xmlDoc.getElementsByTagName("Gender_Score")[0].textContent 返回上面第一个 SQLQuery 节点集的性别分数。

但是,我不太清楚如何查看第二个 SQL 查询节点集以找到 WLAppetiteID = 2 的对,然后获取 WLPetite 的值。

我真的不知道从哪里开始。GetElementsByTagName 只是为我提供了所有 WLAppetiteID 标记的 html 集合,而不管 NodeLevel 是什么。我确实尝试过使用 IndexOf 或包含在内,但这似乎无效。

非常感谢任何帮助或指示。

标签: javascriptxml

解决方案


您问题中的 xml 格式不正确(例如,它缺少结束<root>标记,但您要查找的内容可以通过以下方式完成:

text = `[your xml, fixed]`
let parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");
path = ".//WLAppetiteID[.=2]/following-sibling::WLAppetite"
target = xmlDoc.evaluate(path, xmlDoc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
console.log(target.snapshotItem(0).textContent);

输出是3


推荐阅读