我有一个关于XPath表达式的W3C规范(EBNF表示法)的初学者问题。该规范可在以下网址找到:http://www.w3.org/TR/xpath/。特别是我有一个关于理解以下表达式的问题:解析XPath表达式了解EBNF生产规则
(//attribute::name | //attribute::id)[starts-with(string(self::node()), "be") or starts-with(string(self::node()), "1")]
这似乎是一个有效的表达式。我验证了使用http://www.freeformatter.com/xpath-tester.html与下面的XML文档:
<documentRoot>
<!-- Test data -->
<?xc value="2" ?>
<parent name="data" >
<child id="1" name="alpha" >Some Text</child>
<child id="2" name="beta" >
<grandchild id="2.1" name="beta-alpha" ></grandchild>
<grandchild id="2.2" name="beta-beta" ></grandchild>
</child>
<pet name="tigger" type="cat" >
<data>
<birthday month="sept" day="19" ></birthday>
<food name="Acme Cat Food" ></food>
</data>
</pet>
<pet name="Fido" type="dog" >
<description>
Large dog!
</description>
<data>
<birthday month="feb" day="3" ></birthday>
<food name="Acme Dog Food" ></food>
</data>
</pet>
<rogue name="is this real?" >
<data>
Hates dogs!
</data>
</rogue>
<child id="3" name="gamma" mark="yes" >
<!-- A comment -->
<description>
Likes all animals - especially dogs!
</description>
<grandchild id="3.1" name="gamma-alpha" >
<![CDATA[ Some non-parsable character data ]]>
</grandchild>
<grandchild id="3.2" name="gamma-beta" ></grandchild>
</child>
</parent>
</documentRoot>
这给了我下面的结果:
Attribute='id="1"'
Attribute='name="beta"'
Attribute='name="beta-alpha"'
Attribute='name="beta-beta"'
这是我不太清楚这EBNF作品的顺序会产生上面的查询。
感谢您的帮助。
太棒了!现在我懂了。非常感谢。我完全错过了FilterExpression规则。我被卡住了“Step = AxisSpecifier NodeTest Predicate”。很快,很好的答复! – user1362700