Subject: Re: [xsl] Using XPath to combine text from multiple nodes? From: JBryant@xxxxxxxxx Date: Wed, 31 Aug 2005 10:25:53 -0500 |
> 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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Using XPath to combine te, David Carlisle | Thread | Re: [xsl] Using XPath to combine te, JBryant |
RE: [xsl] Generating an overlib pop, cknell | Date | Re: [xsl] Generating an overlib pop, Achim Stäbler |
Month |