java - How can I get array of elements, including missing elements, using XPath in XSLT? -
given following xml-compliant html:
<div>  <a>a1</a>  <b>b1</b> </div>  <div>  <b>b2</b> </div>  <div>  <a>a3</a>  <b>b3</b>  <c>c3</c> </div> doing //a return:
[a1,a3] the problem above third column data in second place, when not found skipped.
how can express xpath elements return:
[a1, null, a3] same case //c, wonder if it's possible get
[null, null, c3] update: consider scenario  no common parents <div>.
<h1>heading1</h1>  <a>a1</a>  <b>b1</b>   <h1>heading2</h1>  <b>b2</b>   <h1>heading3</h1>  <a>a3</a>  <b>b3</b>  <c>c3</c> update: able use xslt well.
there no null value in xpath. there's semi-related question here explains this: http://www.velocityreviews.com/forums/t686805-xpath-query-to-return-null-values.html
realistically, you've got 3 options:
- don't use xpath @ all.
- use this: //a | //div[not(a)], returndivelement if there noawithin it, , have java code handlediv's returned 'noaelement present'. depending on context, may allow output more useful if required, you'll have access entire contents of div, example error 'noaelement found in div (some identifier)'.
- preprocess xml xslt inserts aelements indivelement not have 1 suitable default.
your second case little tricky, , honest, i'd recommend not using xpath @ all, can done:
//a | //h1[not(following-sibling::a) or generate-id(.) != generate-id(following-sibling::a[1]/preceding-sibling::h1[1])]
this match a elements, or h1 elements no following a element exists before next h1 element, or end of document. dimitre pointed out though, works if you're using within xslt, generate-id xslt function.
if you're not using within xlst, can use rather contrived formula:
//a | //h1[not(following-sibling::a) or count(. | preceding-sibling::h1) != count(following-sibling::a[1]/preceding-sibling::h1)]
it works matching h1 elements count of , preceding h1 elements not same count of h1 elements preceding next a. there may more efficient way of doing in xpath, if it's going more contrived that, i'd recommend not using xpath @ all.
Comments
Post a Comment