这是我的XML文件: -
<products>
<product_id value="1">
<tab_id value="351">
<tab_name value="test1" />
<region_timezone value="1" />
<registrationstatus value="2" />
<eventstatus value="2" />
<dist_activity value="5,10068,10070" />
<reg_str_dt value="2013-01-14 20:35:00" />
<reg_end_dt value="2013-01-14 20:35:00" />
</tab_id>
</product_id>
<product_id value="2">
<tab_id value="352">
<tab_name value="test2" />
<region_timezone value="1" />
<registrationstatus value="2" />
<eventstatus value="2" />
<dist_activity value="5,10069,10070" />
<reg_str_dt value="2013-02-14 20:39:00" />
<reg_end_dt value="2013-02-14 20:39:00" />
</tab_id>
</product_id>
</products>
这是功能: -
function get_list(){
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
$title = $xml.find('[value="'+$('#select').val()+'"] *');
var str='';
$title.each(function(){
str+=$(this).attr('value')+' ';
});
$("#result").html(str);
}
这是Html代码: -
<input type="text" id="select">
<input type="button" name="button" value="Search" onclick="get_list()" >
<div id="result">
</div>
这里我有一个文本框
在文本框中输入xml文件EG的任何子attr值: -
first text box:- Dist_activity(tag name)
second textbox:- 5(attr value)
然后单击按钮搜索并显示第一个元素值EG: -
in div "Result":- 1(is product_id value)
这里可以使用xpath和xquery然后对我来说也是最好的。 感谢...
答案 0 :(得分:0)
这样的事情怎么样:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
var xml;
$.get(
"SOXml_Converted.xml",
null,
function (data) {
xml = data;
},
"xml"
);
function get_list() {
var elName = $('#select').val();
var value = $('#value').val();
var xPath = '//*[local-name() = "' + elName + '" and ' +
' contains(concat(@value, ","), "' + value + ',")]' +
'/../../@value';
var iterator = xml.evaluate(xPath, xml.documentElement, null,
XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
var thisNode = iterator.iterateNext();
var str = '';
while (thisNode) {
if (str) {
str += ', ';
}
str += thisNode.textContent;
thisNode = iterator.iterateNext();
}
$("#result").text(str);
}
</script>
</head>
<body>
<input type="text" id="select">
<input type="text" id="value">
<input type="button" name="button" value="Search" onclick="get_list()">
<div id="result">
</div>
</body>
</html>
当然,它不会找到值为“Dist_activity”和“5”的任何内容,因为没有名称为“Dist_activity”的元素,甚至没有值为5的dist_activity
元素
答案 1 :(得分:0)
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
var xml;
$.get(
"xml_converted.xml",
function(data) { xml=data; },
"html"
);
function get_list(){
var xPath = '//*[local-name() = "' + $('#select').val() + '" and @value = "' +
$('#value').val() + '"]/..';
var iterator = document.evaluate(xPath, documentNode, null,
XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null );
var thisNode = iterator.iterateNext();
var str = '';
while (thisNode) {
str += thisNode.textContent ;
thisNode = iterator.iterateNext();
}
$("#result").html(str);
}
</script>
</head>
<input type="text" id="select">
<input type="text" id="value">
<input type="button" name="button" value="Search" onclick="get_list()" >
<div id="result">
</div>
</html>