AR <ar********@yahoo.com> wrote:
Hi,
How can I get strings separately which contain text nodes of a given
element using XSL and XPath?
For example,
<a>
aaa 111
<b>
bbb
<c>
ccc
</c>
BBB
</b>
AAA 222
</a>
For element <a> I need "aaa 111" and "AAA 222" separately.
Thanks in advance
In Bash shell with Expat interface, you can do
start() # Usage: start tag att=value ...
{
case $1 in
a) out=(); i=0 ;;
*) i=$((i + 1)) ;;
esac
}
data() # Usage: data text
{
case ${XML_ELEMENT_STACK[1]} in
a) strcat out[i] "$*" ;;
esac
}
end() # Usage: end tag
{
case $1 in
a) declare -p out ;;
esac
}
xml -s start -d data -e end '<a>...</a>'
Basic idea is to collect contiguous text in the same array element.
out[0]='... aaa 111 ...'
out[2]='... AAA 222 ...'
If you want to strip whitespaces and re-index, then
out=( "${out[@]|.strip}" )
which gives you
out[0]='aaa 111'
out[1]='AAA 222'
--
William Park <op**********@yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html