Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 28th, 2005, 04:13 AM
Registered User
 
Join Date: Feb 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default select question

Hi,

I have a easy XSL question, I would like to transform this XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<lf>
     <a>this is a link <b url="google.com">google</b> to a search engine</a>
</lf>

To get this:

<?xml version="1.0" encoding="UTF-8"?>
<text>this is a link to a search engine</text>
<link url="google.com">google</link>

I have done this XSL:

<xsl:template match="a">
    <xsl:element name="text">
        <xsl:value-of select="text()"/>
     </xsl:element>
   <xsl:apply-templates select="b"/>
</xsl:template>

<xsl:template match="b">
    <xsl:element name="link">
        <xsl:attribute name="url"><xsl:value-of select="@url"/></xsl:attribute>
        <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>

The result is

<?xml version="1.0" encoding="UTF-8"?>
<text>this is a link </text>
<link url="google.com">google</link>


But I lost the end of the text of the <a> tag ("to a search engine").
Any idea?
Thanks
Seb


 
Old February 28th, 2005, 05:10 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

A quick fix, replace:
Code:
<xsl:value-of select="text()"/>
with
Code:
<xsl:apply-templates select="text()"/>
Your styleshhet could be simplified, if you don't need full control over the result elements, by using literal result elements:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="a">
    <text>
      <xsl:apply-templates select="text()"/>  
    </text>
    <xsl:apply-templates select="b"/>
  </xsl:template>

  <xsl:template match="b">
    <link url="{@url}">
      <xsl:value-of select="."/>
    </link>
  </xsl:template>
</xsl:stylesheet>
--

Joe (Microsoft MVP - XML)
 
Old February 28th, 2005, 10:10 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Don't use

<xsl:value-of select="text()"/>

use

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

The first selects only the first text node child, which you almost never want.

Alternatively, you might want <xsl:copy-of select="."/> or <xsl:apply-templates/> - that depends on your requirements


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Question on select statement RinoDM SQL Server 2000 3 January 19th, 2007 10:21 PM
General SELECT question rsearing SQL Server 2005 1 January 16th, 2007 12:01 PM
Select Distinct Question DeMoNN SQL Server 2000 4 November 1st, 2006 02:52 PM
Select Distinct Question DeMoNN Access 4 November 1st, 2006 11:15 AM
Select Distinct Question acdsky SQL Server 2000 6 October 31st, 2006 05:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.