简体   繁体   English

使用javascript xpath检查字符串值和xml属性

[英]check string value with xml attribute using javascript xpath

this is a part of xml:- 这是xml的一部分: -

<products>
  <product_id value="1">
    <tab_id value="351">
      <tab_name value="test1"/>
      <date value="2013\05\10 12:30:00" />
    </tab_id>
  </product_id>
  <product_id value="2">
    <tab_id value="352">
      <tab_name value="test2"/>
      <date value="2013\05\12 12:00:00" />
    </tab_id>
  </product_id>
</products>

i have following string in one variable:- 我在一个变量中有以下字符串: -

var result_date="2013\05\10,2013\05\11,2013\05\12,2013\05\13";

here i want to check result_date every date with the xml file element date . 在这里,我想用xml文件元素date检查result_date每个date
using javascript and xpath. 使用javascript和xpath。
if result_date is match in xml date then return there product_id 如果result_date在xml date匹配,则返回product_id
i dont know how to check yyyy/mm/dd hh:mm:ss to yyyy/mm/dd 我不知道怎么检查yyyy/mm/dd hh:mm:ssyyyy/mm/dd
expected output:- 预期产量: -

 1,2

this both are product_id IN MY xml file. 这两者都是我的xml文件中的product_id
help me out with this. 帮我解决这个问题。
thansk thansk

The xpaht expression you are looking for should be something like: 你正在寻找的xpaht表达式应该是这样的:

var path="//product_id[contains('" + result_date+ "',substring-before(date/@value ,' '))]";

Searching any product_id where the substring-before the blank in date is part of your variable result_date. 搜索任何product_id,其中日期中空白之前的子字符串是变量result_date的一部分。

Most browser support "evaluate" (but not IE). 大多数浏览器支持“评估”(但不支持IE)。

var nodes=xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
var result=nodes.iterateNext();
while (result)
  {
  document.write(result.attributes.value.nodeValue);

  document.write("<br>");
  result=nodes.iterateNext();
  }

If your "result_date" is a const string. 如果你的“result_date”是一个const字符串。 You have to be aware of escaping. 你必须意识到逃避。

var result_date='2013\\05\\10,2013\\05\\11,2013\\05\\12,2013\\05\\13';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM