RE: [xsl] Getting the text node

Subject: RE: [xsl] Getting the text node
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 12 Oct 2005 22:40:43 +0100
> Hi all,
>   I have a beginners question regarding capturing of
> text node and tags within it, with this example:

Well (a) you can't have elements within a text node, and (b) nodes appear in
the tree view of an XML document, while tags appear only in the lexical
view, so you're badly mixing up your concepts.

If the lexical view is

<span>Some <bold>strong</bold> text</span>

then the tree view contains two elements (span and bold) and three text
nodes ("Some ", "strong", and " text"). 
> 
> <root>
> <p>
> <span>
> <field>Data1</field>
> </span>Some bold <b>text here</b>.
> </p>
> <p>
> <span>
> <field>Data2</field>
> </span>Sample data that needs to appear <b>bold</b>
> and <it>ital</it>.
> </p>
> </root>
> 
> output:
> 
> <Data1>Some bold <bold>text here</bold>.</Data1>
> <Data2>Sample data that needs to appear
> <bold>bold</bold> and <ital>ital</ital>.</Data2>
> 
> here is XSL:
> 
> <xsl:template match="p">
>         <xsl:apply-templates select="span"/>
> </xsl:template>
> 
> <xsl:template match="span">
>  <xsl:element name="{field/text()}">
>     <xsl:value-of select="following::text()[1]"/>
>  </xsl:element>
> </xsl:template>

Replace

<xsl:value-of select="following::text()[1]"/>

with

<xsl:copy-of select="following-sibling::node()"/>

because

(a) you only want to copy siblings of the span element (children of the same
p element), not everything in the rest of the document (which is what
following:: does)

(b) you want to copy elements (such as the bold element) as well as text
nodes, and

(c) you want to copy the nodes as a tree structure rather than reducing them
all to plain text which is what xsl:value-of does.

Michael Kay
http://www.saxonica.com/

Current Thread