Strings of an xml element using XSL and XPath

A

AR

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
 
M

Martin Honnen

AR wrote:

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.

/a/text()
selects those text nodes which are child nodes of the <a> element.
As for processing them seperately you can use xsl:for-each
select="/a/text()" or xsl:apply-templates select="/a/text()".
 
W

William Park

AR said:
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 "$*" ;;
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'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top