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

N

nobody

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)
 
M

Martin Honnen

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
 

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,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top