Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > [Q] XPath/XSL: how to get "position" of "embedded elem in mixed content?

Reply
Thread Tools

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

 
 
nobody
Guest
Posts: n/a
 
      07-18-2004
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>
<xsltherwise>
<!-- x in the middle -->
<xsl:text>+</xsl:text>
<xsl:value-of select="."/>
<xsl:text>+</xsl:text>
</xsltherwise>
</xsl:choose>
</xsl:template>

thank you very much! andreas (ala_NO@SPAM_context.ch)
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      07-18-2004


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
xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xslutput 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>
<xsltherwise>
<xsl:value-of select="." />
</xsltherwise>
</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/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I use Hpricot to parse data into different array elem? Christiaan Venter Ruby 1 05-22-2009 05:11 AM
Hpricot elem index/position henryturnerlists@googlemail.com Ruby 7 10-08-2008 07:05 PM
extract value of the hpricot elem Junkone Ruby 1 08-12-2008 07:25 PM
insert an elem into a link list neilcancer@gmail.com C Programming 4 04-04-2006 06:59 AM
cross-browser object/elem access Matt Javascript 2 05-07-2004 04:04 AM



Advertisments