[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Using XPath to combine text from multiple nodes?

Subject: Re: Using XPath to combine text from multiple nodes?
From: JBryant@xxxxxxxxx
Date: Wed, 31 Aug 2005 10:25:53 -0500
xpath select multiple nodes
> 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)

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.