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 6th, 2007, 06:31 AM
Registered User
 
Join Date: Apr 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to get data from top level elements

Hi

I'm quite new to XSL and I've run in to some trouble.


I have some data on an existing XML format:
<?xml version="1.0" standalone="yes"?>
<root>
<field>
    <name>MyField1</name>
    <dtype>string</dtype>
    <key>true</key>
</field>
<field>
    <name>MyField2</name>
    <dtype>int</dtype>
    <key>false</key>
</field>
<data>
    <name>MyField1</name>
    <class>Data1</type>
    <assembly>MyNameSpc.Juba</assembly>
</data>
<data>
    <name>MyField1</name>
    <class>Data2</type>
    <assembly>MyNameSpc.Juba</assembly>
</data>
<data>
    <name>MyField2</name>
    <class>Data3</class>
    <assembly>MyNameSpc.Juba</assembly>
</data>
</root>


Now, I want to transform this into something like this:
<?xml version="1.0" encoding="UTF-8"?>
<root>
 <fields>
   <field name="MyField1" dtype="string" key="true">
     <datainfo>
      <data class="Data1" assembly="MyNameSpc.Juba"/>
      <data class="Data2" assembly="MyNameSpc.Juba"/>
     </datainfo>
   </field>
   <field name="MyField2" dtype="int" key="false">
     <datainfo>
      <data class="Data3" assembly=="MyNameSpc.Juba"/>
     </datainfo>
   </field>
 <fields>
</root>

Here is my XSL so far:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="root">
 <root>
   <xsl:apply-templates select="field"/>
 </root>
</xsl:template>
<xsl:template match="field">
  <fields>
   <xsl:for-each select="field">
    <xsl:apply-templates select="field"/>
   </xsl:for-each>
  </fields>
</xsl:template>
<xsl:template match="field">
  <field>
   <xsl:attribute name="name"><xsl:value-of select="name"/></xsl:attribute>
   <xsl:attribute name="dtype"><xsl:value-of select="dtype"/></xsl:attribute>
   <xsl:attribute name="key"><xsl:value-of select="key"/></xsl:attribute>
   <xsl:apply-templates select="data"/>
  </field>
</xsl:template>

<xsl:template match="data">
  <datainfo>
    <xsl:for-each select="data">
      <xsl:apply-templates select="data"/>
    </xsl:for-each>
  </datainfo>
</xsl:template>

<xsl:template match="data">
    <xsl:attribute name="class"><xsl:value-of select="class"/></xsl:attribute>
    <xsl:attribute name="assembly"><xsl:value-of select="assembly"/></xsl:attribute>
</xsl:template>
</xsl:stylesheet>

This produces
<root>
 <fields>
   <field name="MyField1" dtype="string" key="true"/>
   <field name="MyField2" dtype="int" key="false"/>
 <fields>
</root>

So my problem here is how to get back up to the root node and get hold of the "data" elements which belongs to the current field name that I'm at. I know that there is something wrong with <xsl:apply-templates select="data"/> under the field element. I guess this is lost under the field element in the original XML element.

On top of that there should be some checking on that the current field name is equal to the name of the data element to be put under the current field.

Any help would be appreciated!

Cheers!
 
Old July 6th, 2007, 07:49 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Firstly, it doesn't make sense to have two template rules that both say match="field". (Or match="data"). This is an error, though processors are allowed to ignore it and simply ignore the first rule.

Secondly, this is nonsense unless you have fields-within-fields-within-fields:

<xsl:template match="field">
  <fields>
   <xsl:for-each select="field">
    <xsl:apply-templates select="field"/>

Your template matches a field, the for-each then selects its children called field, and for each of these children the apply-templates processes the grandchildren called field...

Finally, to get from a field to a data element, you can't simply use

<xsl:apply-templates select="data"/>

That expects the data to be a child of the field. You need

<xsl:apply-templates select="/root/data[name=current()/name]"/>

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
.level and .data files question H2O BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 0 September 17th, 2007 06:03 AM
User Level Data Caching ghari ASP.NET 1.0 and 1.1 Professional 11 August 14th, 2007 12:06 PM
Shared Data source Vs connection on report level mverma4you Reporting Services 0 June 6th, 2007 01:47 AM
xml invalid top level from ASP write XML(solution) g000we XML 0 August 9th, 2006 03:56 AM
Non-recursive Data multi-level grouping jmurdock BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 June 30th, 2004 03:42 PM





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