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 July 5th, 2007, 06:15 AM
Registered User
 
Join Date: May 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML to XML with xslt problem

Hello, It's been a while since I've worked with XSLT, so I've been staring at this all day trying to get my head around it.

I have an XML file in format:
Code:
<Mapping>
<Lang id="1">
<Field name="firstname" value = "First Name"/>
<Field name="lastname" value = "Last Name"/>
<Field name="optionalfield" value = "Optional Field"/>
</Lang>
<Lang id="2">
<Field name="firstname" value = "Voornaam"/>
<Field name="lastname" value = "Familienaam"/>
</Lang>
</Mapping>
this XML document needs to be transformed to following format:
Code:
<Mapping>
<Field name="firstname">
<Value languageId="1">First Name</Value>
<Value languageId="2">Voornaam</Value>
</Field>
<Field name="optionalField">
<Value languageId="1">Optional Field</Value>
<Value languageId="2"></Value>
</Field>
</Mapping>
Now I can't get to transform to this format.



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

I'm not sure where you had difficulties, so it's hard to know where you need help. It's always a good idea to show your code as that reveals what you know and what you don't know. (That's probably why so many beginners fail to do it - people with more experience are much more prepared to admit their limitations).

Your example isn't really a complete specification, for example I can't see any logic to why "firstname" is included in the output and "lastname" isn't. I assume you were just cutting corners.

I think you want something like this:

<xsl:template match="Mapping">
<xsl:variable name="mapping" select="."/>
<Mapping>
  <xsl:for-each select="Lang[1]/Field">
    <xsl:variable name="name" select="@name"/>
    <Field name="{$name}">
      <xsl:for-each select="$mapping/Lang">
         <Value languageid="{@id}">
           <xsl:value-of select="Field[@name=$name]/@value"/>



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
xml and xsl templates as input to xslt gives xml rameshnarayan XSLT 5 August 3rd, 2005 01:58 AM
XSLT for complicated xml to xml transf. required doug@sirvisetti XSLT 3 June 17th, 2005 04:26 PM
merge two xml file and make new xml using xslt ketan XSLT 0 September 21st, 2004 08:48 AM
xml and xslt problem astroflake XSLT 1 April 13th, 2004 06:09 AM
Merge XML files into a xml file using xslt lxu XML 4 November 6th, 2003 06:01 PM





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