开发者

How to simplify this Xpath expression?

开发者 https://www.devze.com 2023-02-04 21:56 出处:网络
I have the following XML code : <root> <a><l_desc/></a> <b><l_desc>foo</l_de开发者_开发知识库sc></b>

I have the following XML code :

<root>
   <a><l_desc/></a>
   <b><l_desc>foo</l_de开发者_开发知识库sc></b>
   <c><l_desc>bar</l_desc></c>
</root>

I want to match the l_desc nodes with a or b nodes as parent.

For now, i use this xpath expression : //a/l_desc/.. | //b/l_desc/..

I would prefer writing something like this : //(a|b)/l_desc/.. Unfortunately, this expression is invalid.

Do you have any idea for reducing the first expression ? The xpath is to be used in an XSLT stylesheet v1.0.

Stéphan


I want to match the l_desc nodes with a or b nodes as parent

More simple:

/root/a/l_desc | /root/b/l_desc

Or

/root/*[self::a | self::b]/l_desc

With starting // operator (bad design, unknown schema):

//a/l_desc | //b/l_desc

Or

//*[self::a | self::b]/l_desc

In XPath 2.0 this is valid

/root/(a|b)/l_desc


How about

//l_desc[parent::a or parent::b]

?


you can use a predicate here

l_desc[../a|../b]


Use:

/*/*[self::a or self::b]/l_desc

This means: Select all l_desc elements that are children of any a or b element that itself is a child of the top element of the XML document.

Avoid:

  1. Using the // abbreviation when you know the structure of the document. // can be very inefficient.

  2. Using a reverse axis (such as parent::) when this is not necessary. Expressions containing reverse axes are more difficult to understand and may be less efficient than expressions containing only forward axes.

0

精彩评论

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