开发者

XPath: How to match a class of characters in an attribute?

I have the following XML fragment:

...

<Setting name="HOST1">server1</Setting>
<Setting name="HOST2">server2</Setting>
<Setting name="HOST3">server4</Setting>
<Setting name="HOST4">server8n12</Setting>
...
<!-- Many more such declarations all with the attribute of the form "HOSTNN" -->

I am using LibXML to find the nodes开发者_运维问答 that match HOST

my $dom = XML::LibXML->load_xml(location => $xml)
    or die "Unable to create LibXML object";
my $root = $dom->getDocumentElement();

my @hostnames = $root->findnodes('//Setting[contains(@name,"HOST")]');

Is it possible to specify a class of characters in an XPath expression similar to what you can specify in a regexp? For example, the Perl regexp:

/^HOST\d+?$/


//Setting[starts-with(@name,"HOST")]
         [translate(substring-after(@name,"HOST"),
                    "0123456789",
                    "")=""]

Edit: I didn't see the qualifier. The above will match Setting[@name="HOST"]. To be more strict:

//Setting[starts-with(@name,"HOST")]
         [number(substring-after(@name,"HOST")) = 
          substring-after(@name,"HOST")]

Edit 2: The above it's not strict enough (It will match "HOST2.1"). Sorry.

//Setting[starts-with(@name,"HOST")]
         [substring-after(@name,"HOST")]
         [translate(substring-after(@name,"HOST"),
                    "0123456789",
                    "")=""]


My xpath is a bit rusty, but I think this may work (either that or i'm combining my css with xpath)

Setting[@name^="HOST"]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜