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:

  1. don't use xpath @ all.
  2. use this: //a | //div[not(a)], return div element if there no a within it, , have java code handle div's returned 'no a element present'. depending on context, may allow output more useful if required, you'll have access entire contents of div, example error 'no a element found in div (some identifier)'.
  3. preprocess xml xslt inserts a elements in div element 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

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -