By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,813 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,813 IT Pros & Developers. It's quick & easy.

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

P: n/a
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:otherwise>
<!-- x in the middle -->
<xsl:text>+</xsl:text>
<xsl:value-of select="."/>
<xsl:text>+</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

thank you very much! andreas (ala_NO@SPAM_context.ch)
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a


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

Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.