Question

My xml file has a line:

<asin ISBN="0001714600 9780001714601" code="aB1234Av">

I want to extract ISBN[0] i.e

0001714600 9780001714601

or ISBN[1] i.e

0001714600 9780001714601

I tried substring and tokenize but its not working to my desire. Heres my code:

<strong>{for $asin in doc('amazon.xml')/asin/@ISBN
    return (tokenize($asin/data(.),'\s'))}</strong>

(OR)

<strong>{for $asin in doc('amazon.xml')/asin/@ISBN
    return (substring($asin/data(.),1))}</strong>

How should i get that value out of it?

Was it helpful?

Solution 2

tokenize(@ISBN, '\s')[1] and tokenize(@ISBN, '\s')[2] should work perfectly well, or you could also do substring-before(@ISBN, ' ') and substring-after(@ISBN, ' ')

OTHER TIPS

Using tokenize() is OK.

If you take things apart, it becomes easier to see what happens:

let $input := <asin ISBN="0001714600 9780001714601" code="aB1234Av"/>
let $isbn-string := $input/@ISBN/string()
let $isbn-items := tokenize($isbn-string, '\s')
    return
        for $isbn-item in $isbn-items
            return <strong>{$isbn-item}</strong>

You first fetch the string value of the @ISBN attribute, then tokenize it, then iterate over the tokenized items, outputting them one by one.

PS: In XPath, positional predicates start with 1, so ISBN[0] would return nothing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top