匹配XML元素中的属性值的最佳方法 [英] Best way to match Attribute value in XML element

查看:106
本文介绍了匹配XML元素中的属性值的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的XML:

< measValue dn="Cabinet=0, Shelf=0, Card=2, Host=0">

    < r p="1">1.42</r>

    < r p="2">2.28</r>

< /measValue>

我想用类似

1> Host=0#这很容易

我的解决方案:

if (getAttribute("dn")=~ /Host=0/)

2> Host=0 && Card=2

我可以做到,但我需要将其匹配两次,例如

if (getAttribute("dn")=~ /Host=0/) && (getAttribute("dn")=~ /Card=2/)

有没有更好的方法来实现与第二种模式的匹配?使用LibXML

Is there any better way to accomplice this match this second pattern? using LibXML

推荐答案

尝试一下:

if (getAttribute("dn")=~ /^(?=.*\bHost=0\b)(?=.*\bCard=2\b)/)

这里的边界\b一词是为了避免与myHost=01和所有相似词匹配.

The word boundaries \b are here to avoid matching myHost=01 and everything similar.

这篇关于匹配XML元素中的属性值的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆