[Q] XPath/XSL: how to get "position" of "embedded elem in mixed content?

Discussion in 'XML' started by nobody, Jul 18, 2004.

  1. nobody

    nobody Guest

    hi there!

    given
    <!ELEMENT a (#PCDATA | x)*>
    <!ELEMENT x (#PCDATA)>
    how can I find out if x is "embedded" at the beginning
    <a><x>xxx</x>aaa</a>
    or at the end
    <a>aaa<x>xxx</x></a>
    or in the middle
    <a>aaa<x>xxx</x>bbb</a>
    of the content of element a?

    what I need to do is to output x according to it's
    "position" within a. examples:
    <a><x>xxx</x>aaa</a> --> "xxx+aaa"
    <a>aaa<x>xxx</x></a> --> "aaa+xxx"
    <a>aaa<x>xxx</x>bbb</a> --> "aaa+xxx+bbb"

    can any body help me with the two test clauses needed?
    <xsl:template match="x">
    <xsl:choose>
    <xsl:when test="???">
    <!-- x at start -->
    <xsl:value-of select="."/>
    <xsl:text>+</xsl:text>
    </xsl:when>
    <xsl:when test="???">
    <!-- x at end -->
    <xsl:text>+</xsl:text>
    <xsl:value-of select="."/>
    </xsl:when>
    <xsl:eek:therwise>
    <!-- x in the middle -->
    <xsl:text>+</xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>+</xsl:text>
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:template>

    thank you very much! andreas (ala_NO@SPAM_context.ch)
     
    nobody, Jul 18, 2004
    #1
    1. Advertising

  2. Re: [Q] XPath/XSL: how to get "position" of "embedded elem in mixedcontent?

    nobody wrote:


    > given
    > <!ELEMENT a (#PCDATA | x)*>
    > <!ELEMENT x (#PCDATA)>
    > how can I find out if x is "embedded" at the beginning
    > <a><x>xxx</x>aaa</a>
    > or at the end
    > <a>aaa<x>xxx</x></a>
    > or in the middle
    > <a>aaa<x>xxx</x>bbb</a>
    > of the content of element a?
    >
    > what I need to do is to output x according to it's
    > "position" within a. examples:
    > <a><x>xxx</x>aaa</a> --> "xxx+aaa"
    > <a>aaa<x>xxx</x></a> --> "aaa+xxx"
    > <a>aaa<x>xxx</x>bbb</a> --> "aaa+xxx+bbb"
    >
    > can any body help me with the two test clauses needed?


    Here is an XSLT 1.0 stylesheet

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:eek:utput method="text" encoding="UTF-8" />

    <xsl:template match="/">
    <xsl:apply-templates select="root/a" />
    </xsl:template>

    <xsl:template match="a">
    <xsl:apply-templates select="node()" mode="add" />
    <xsl:text>
    </xsl:text>
    </xsl:template>

    <xsl:template match="x | text()" mode="add">
    <xsl:choose>
    <xsl:when test="position() &lt; last()">
    <xsl:value-of select="." />
    <xsl:text>+</xsl:text>
    </xsl:when>
    <xsl:eek:therwise>
    <xsl:value-of select="." />
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:template>

    </xsl:stylesheet>

    that transforms

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
    <a><x>xxx</x>aaa</a>
    <a>aaa<x>xxx</x></a>
    <a>aaa<x>xxx</x>bbb</a>
    </root>

    into the following text:

    xxx+aaa
    aaa+xxx
    aaa+xxx+bbb

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jul 18, 2004
    #2
    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. insert an elem into a link list

    , Apr 4, 2006, in forum: C Programming
    Replies:
    4
    Views:
    320
    CBFalconer
    Apr 4, 2006
  2. Junkone
    Replies:
    1
    Views:
    105
    Mark Thomas
    Aug 12, 2008
  3. Replies:
    7
    Views:
    174
  4. Christiaan Venter
    Replies:
    1
    Views:
    153
    7stud --
    May 22, 2009
  5. Matt

    cross-browser object/elem access

    Matt, May 7, 2004, in forum: Javascript
    Replies:
    2
    Views:
    92
    Richard Cornford
    May 7, 2004
Loading...

Share This Page