Project

Profile

Help

Convert Sequence such that it consists of atomic values

Added by Michael Staal-Olsen over 4 years ago

Say I have some Sequence object 'sec', which comes from an XPath expression. The expression may valuate to a series of nodes. Is there a standard way to convert this sequence to an AtomicValue counterpart consisting of the string values of the XML instead, so that the remaining XML structure is removed? The reason I ask is because I want to use the Saxon DistinctValues class, which requires my Sequence to give rise to a SequenceIterator that returns AtomicValues.


Replies (4)

Please register to reply

RE: Convert Sequence such that it consists of atomic values - Added by Michael Kay over 4 years ago

On the XPath side, you could wrap the expression in a call on the data() function.

From a Sequence seq, the best way is probably

AtomizingIterator iter = new AtomizingIterator(seq.iterate())

This gives you the typed value rather than the string value, but there's no difference unless your XPath expression is schema-aware.

RE: Convert Sequence such that it consists of atomic values - Added by Michael Kay over 4 years ago

More generally, you could do

SequenceIterator stringValues = 
    new ItemMappingIterator(seq.iterate(), item->new StringValue(item.getStringValue()));

RE: Convert Sequence such that it consists of atomic values - Added by Michael Staal-Olsen over 4 years ago

Thanks! But what then if I want to construct a Sequence from the SequenceIterator? And in what sense is the last solution more general? Do I lose structure via that solution?

RE: Convert Sequence such that it consists of atomic values - Added by Michael Kay over 4 years ago

To construct a Sequence from a SequenceIterator, use new SequenceExtent(), or new LazySequence(), depending on whether you want the SequenceIterator to be immediately evaluated.

By "more general" I meant that the ItemMappingIterator can be parameterised with any one-to-one mapping function, this particular function is just a special case.

    (1-4/4)

    Please register to reply