Strings of an xml element using XSL and XPath

Discussion in 'XML' started by AR, May 27, 2005.

  1. AR

    AR Guest

    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
    AR, May 27, 2005
    #1
    1. Advertising

  2. 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()".

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, May 27, 2005
    #2
    1. Advertising

  3. AR

    William Park Guest

    AR <> 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 "$*" ;;
    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 <>, Toronto, Canada
    ThinFlash: Linux thin-client on USB key (flash) drive
    http://home.eol.ca/~parkw/thinflash.html
    William Park, May 27, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pat Turner
    Replies:
    4
    Views:
    557
    Pat Turner
    Dec 6, 2004
  2. Replies:
    1
    Views:
    3,585
    A. Bolmarcich
    May 27, 2005
  3. Ian Wilson
    Replies:
    2
    Views:
    544
    Ian Wilson
    Jul 26, 2007
  4. Kniffel
    Replies:
    8
    Views:
    1,944
    Kniffel
    Sep 7, 2007
  5. HANM
    Replies:
    2
    Views:
    695
    Joseph Kesselman
    Jan 29, 2008
Loading...

Share This Page