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 April 25th, 2006, 05:56 AM
Authorized User
 
Join Date: Apr 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML to XML

I'm fairly new to XSLT, so this is likely to be the first of many questions!

Using XSLT I'm trying to convert the following lines of XML:

<Type>Office</Type>
<PhoneNbr>123456789</PhoneNbr>

into:

<Contact Type="Office">123456789</Contact>

any ideas?

thanks in advance


 
Old April 25th, 2006, 06:59 AM
Authorized User
 
Join Date: Apr 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Hi, try some thing like this:


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:template match="/">
  <Contact>
   <xsl:attribute name="Type" select="Type/text()" />
   <xsl:value-of select="PhoneNbr/text()" />
  </Contact>
 </xsl:template>
</xsl:stylesheet>


 
Old April 25th, 2006, 07:20 AM
Authorized User
 
Join Date: Apr 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks Shasto100, but I had already tried this & it came up with "The 'Select' attribute is not declared"

 
Old April 25th, 2006, 07:58 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

For your input to be well-formed XML it must have some wrapper element, let's assume it is:

<Phone>
<Type>Office</Type>
<PhoneNbr>123456789</PhoneNbr>
</Phone>

Then you simply write:

<xsl:template match="Phone">
<Contact Type="{Type}"><xsl:value-of select="PhoneNbr"/></Contact>
</xsl:template>




Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 25th, 2006, 08:00 AM
Authorized User
 
Join Date: Apr 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I thought it may have something to do with the OUTPUT element also, eg:

<xsl:output method="xml/text"/>

But its best listening to Michael. hehe

 
Old April 25th, 2006, 08:06 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If the error message was

"The 'Select' attribute is not declared"

then perhaps you wrote "Select" rather than "select". XML and XSLT are case-sensitive.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 25th, 2006, 09:08 AM
Authorized User
 
Join Date: Apr 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Brilliant.

Many thanks again






Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Server 2005 XML: FOR XML PATH -> cdata? stoves SQL Server 2005 1 July 8th, 2008 02:40 AM
Creating XML doc ; writing string(xml format) into KamalRaturi XML 5 May 28th, 2008 05:51 AM
VB.net, adding XML data to an existing XML file saikoboarder XML 11 April 17th, 2008 04:19 PM
xml invalid top level from ASP write XML(solution) g000we XML 0 August 9th, 2006 03:56 AM
DTS Package, XML task. Read XML file and store it Victoria SQL Server DTS 0 July 24th, 2006 02:43 PM





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