xsl-list
[Top] [All Lists]

Re: Using XPath to combine text from multiple nodes?

2005-08-31 08:25:53
Is there any way to write an XPath expression that
will combine the text within two nodes and return
a text node that contains the combined strings.

Yes.

<Employee first="Mark" second="Allanson" />
 
What I would like to do in this case is to
effectively return a node that contains the text
"MarkAllanson". 

Here's one (of many) ways:

<xsl:template match="Employee">
  <xsl:value-of select="@first"/></xsl:value-of select="@second"/>
</xsl:template>

Here's a way with the concat function:

<xsl:template match="Employee">
  <xsl:value-of select="concat(@first, @second)"/>
</xsl:template>

<Work>
 <Employee first="Mark" last="Allanson" />
 <Address>
    <City>London</City>
 </Address
</Work>
 
Is it possible to write an XPath to return a node
list that contains both the first attribute of the
employee and the City element of the address?

Yes.

<xsl:template match="Work">
  <xsl:value-of select="Employee/@first"/><xsl:value-of 
select="Address/City"/>
</xsl:template>

Of course, you can get those values from within other templates by using 
different XPath expressions. For example, suppose you want to get the city 
while processing the Employee node. You could use

<xsl:value-of select="../Address/City"/>

Also, bear in mind that you don't have to use value-of. Since the content 
I need to transform often contains embedded elements (to indicate bold and 
italics and such), I rarely use value-of and instead apply templates, so 
that I can pick up the formatting elements, thus:

<xsl:template match="Employee">
  <xsl:value-of select="@first"/>
  <xsl:apply-templates select="../Address/City"/>
</xsl:template>

<!-- other templates go here, to handle the other nodes -->

<xsl:template match="City">
  <xsl:apply-templates/>
</xsl:template>

Then, if an author had included a <bold> element somewhere, I'd pick it up 
and process it, which wouldn't happen with value-of.

Jay Bryant
Bryant Communication Services
(presently consulting at Synergistic Solution Technologies)

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--