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 June 12th, 2006, 08:46 AM
Authorized User
 
Join Date: Jun 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with repeat data in XML to XML transformat

I have xml that looks like this

Code:
<pavement_x0020_Conditions>
<NetworkID>EVERETT</NetworkID>
<Name>SNOHOMISH COUNTY (PAINE FIELD)</Name>
<BranchID>AEHPF</BranchID>
<SectionID>01</SectionID>
<Surface>AC</Surface>
<Condition>94</Condition>
<DATE>2005-03-03T00:00:00</DATE>
<SAMPLES>37</SAMPLES>
</pavement_x0020_Conditions>

<pavement_x0020_Conditions>
<NetworkID>PUYALLUP</NetworkID>
<Name>PIERCE COUNTY - THUN FIELD</Name>
<BranchID>A02TH</BranchID>
<SectionID>03</SectionID>
<Surface>AC</Surface>
<Condition>77</Condition>
<DATE>2005-03-03T00:00:00</DATE>
<SAMPLES>6</SAMPLES>
</pavement_x0020_Conditions>
<pavement_x0020_Conditions>
<NetworkID>PUYALLUP</NetworkID>
<Name>PIERCE COUNTY - THUN FIELD</Name>
<BranchID>AH1TH</BranchID>
<SectionID>01</SectionID>
<Surface>AC</Surface>
<Condition>98</Condition>
<DATE>2005-03-03T00:00:00</DATE>
<SAMPLES>2</SAMPLES>
</pavement_x0020_Conditions>
<pavement_x0020_Conditions>
<NetworkID>PUYALLUP</NetworkID>
<Name>PIERCE COUNTY - THUN FIELD</Name>
<BranchID>A02TH</BranchID>
<SectionID>02</SectionID>
<Surface>AC</Surface>
<Condition>78</Condition>
<DATE>2005-03-03T00:00:00</DATE>
<SAMPLES>7</SAMPLES>
</pavement_x0020_Conditions>
and I need to transform it to this (the data changes, obviously. Two different databases)

Code:
<airports>

<airport name="Benson">
<map>data/benson.swf</map>
<branches>

<pavement_x0020_Branch>
<DATE>2003-01-25T00:00:00</DATE>
<SAMPLES>28</SAMPLES>
<BranchID>A01BN</BranchID>
<SectionID>10</SectionID>
<Surface>AC</Surface>
<Condition>100</Condition>
</pavement_x0020_Branch>

<pavement_x0020_Branch>
<DATE>2003-01-25T00:00:00</DATE>
<SAMPLES>55</SAMPLES>
<BranchID>RW1028BN</BranchID>
<SectionID>10</SectionID>
<Surface>AC</Surface>
<Condition>99</Condition>
</pavement_x0020_Branch>

<pavement_x0020_Branch>
<DATE>2003-01-25T00:00:00</DATE>
<SAMPLES>1</SAMPLES>
<BranchID>TWABN</BranchID>
<SectionID>10</SectionID>
<Surface>AC</Surface>
<Condition>98</Condition>
</pavement_x0020_Branch>

</branches>
</airport>

<airport name="Bisbee">
<map>data/bisbee.swf</map>
<branches>

<pavement_x0020_Branch>
<DATE>2003-01-25T00:00:00</DATE>
<SAMPLES>59</SAMPLES>
<BranchID>RW1735BM</BranchID>
<SectionID>10</SectionID>
<Surface>AAC</Surface>
<Condition>90</Condition>
</pavement_x0020_Branch>

<pavement_x0020_Branch>
<DATE>2003-01-25T00:00:00</DATE>
<SAMPLES>50</SAMPLES>
<BranchID>TWABM</BranchID>
<SectionID>10</SectionID>
<Surface>AC</Surface>
<Condition>79</Condition>
</pavement_x0020_Branch>

<pavement_x0020_Branch>
<DATE>2003-01-25T00:00:00</DATE>
<SAMPLES>22</SAMPLES>
<BranchID>A01BM</BranchID>
<SectionID>10</SectionID>
<Surface>AC</Surface>
<Condition>90</Condition>
</pavement_x0020_Branch>

</branches>
</airport>
</airports>
Here is the problem: I basically have to make <NetworkID> from the the first snippet into <airport> from the second (the other nodes should stay the same). But if I make <NetworkID> a parent node I think I will get something that looks like this:

Code:
<airport name="Benson">
<map>data/benson.swf</map>
<branches>

<pavement_x0020_Branch>
<DATE>2003-01-25T00:00:00</DATE>
<SAMPLES>28</SAMPLES>
<BranchID>A01BN</BranchID>
<SectionID>10</SectionID>
<Surface>AC</Surface>
<Condition>100</Condition>
</pavement_x0020_Branch>

</branches>
</airport>

<airport name="Benson">
<map>data/benson.swf</map>
<branches>

<pavement_x0020_Branch>
<DATE>2003-01-25T00:00:00</DATE>
<SAMPLES>55</SAMPLES>
<BranchID>RW1028BN</BranchID>
<SectionID>10</SectionID>
<Surface>AC</Surface>
<Condition>99</Condition>
</pavement_x0020_Branch>

</branches>
</airport>

<airport name="Benson">
<map>data/benson.swf</map>
<branches>

<pavement_x0020_Branch>
<DATE>2003-01-25T00:00:00</DATE>
<SAMPLES>1</SAMPLES>
<BranchID>TWABN</BranchID>
<SectionID>10</SectionID>
<Surface>AC</Surface>
<Condition>98</Condition>
</pavement_x0020_Branch>

</branches>
</airport>
Do you see my problem? I need some way to tell the transformer not to make a new <airport> node if the <NetworkID> is the same as the previous sibling. I tried using <xsl:variable> and <xsl:choose>, but I'm afraid my know-how is a little limited.

Thanks in advance for any help

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

>I need some way to tell the transformer not to make a new <airport> node if the <NetworkID> is the same as the previous sibling.

I haven't looked at your problem in detail (please in future try to cut it down to the essentials!) but the above sentence suggests you are doing grouping. In XSLT 2.0 this is done using the new <xsl:for-each-group> construct. In XSLT 1.0 it's more difficult, but there are well-known techniques described at http://www.jenitennison.com/xslt/grouping

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 12th, 2006, 09:28 AM
Authorized User
 
Join Date: Jun 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yeah, sorry about that. I get excited:)

I'm onto the grouping thing. Thanks for pointing me in the right direction. XSLT is a little daunting at first, so your help and time are appreciated.

 
Old June 13th, 2006, 08:45 AM
Authorized User
 
Join Date: Jun 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

For the record, here is what I came up with for my xsl:

Code:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
  <xsl:template match="dataroot">
<airports>
    <xsl:for-each-group select="pavement_x0020_Conditions" group-by="Name">
      <xsl:sort select="Name"/>
      <airport name="{Name}">
    <map>data/<xsl:value-of select="Name"/>.swf</map>
        <xsl:for-each-group select="current-group()" group-by="NetworkID">
    <branches>
        <xsl:for-each select="current-group()">

<pavement_x0020_Branch>
        <xsl:copy-of select="DATE"/>
        <xsl:copy-of select="SAMPLES"/>        
            <xsl:copy-of select="BranchID"/>
               <xsl:copy-of select="SectionID"/>
        <xsl:copy-of select="Surface"/>
        <xsl:copy-of select="Condition"/>

</pavement_x0020_Branch>
        </xsl:for-each>
          </branches>
        </xsl:for-each-group>
      </airport>
    </xsl:for-each-group>
</airports>
  </xsl:template>
</xsl:stylesheet>
The only difference in the xml file that I processed with this file and the one I posted in the previous post is a <dataroot> </dataroot> wrapper that MS Access 2003 puts around all of its xml exports.

 
Old June 13th, 2006, 08:45 AM
Authorized User
 
Join Date: Jun 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

For the record, here is what I came up with for my xsl:

Code:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
  <xsl:template match="dataroot">
<airports>
    <xsl:for-each-group select="pavement_x0020_Conditions" group-by="Name">
      <xsl:sort select="Name"/>
      <airport name="{Name}">
    <map>data/<xsl:value-of select="Name"/>.swf</map>
        <xsl:for-each-group select="current-group()" group-by="NetworkID">
    <branches>
        <xsl:for-each select="current-group()">

<pavement_x0020_Branch>
        <xsl:copy-of select="DATE"/>
        <xsl:copy-of select="SAMPLES"/>        
            <xsl:copy-of select="BranchID"/>
               <xsl:copy-of select="SectionID"/>
        <xsl:copy-of select="Surface"/>
        <xsl:copy-of select="Condition"/>

</pavement_x0020_Branch>
        </xsl:for-each>
          </branches>
        </xsl:for-each-group>
      </airport>
    </xsl:for-each-group>
</airports>
  </xsl:template>
</xsl:stylesheet>
The only difference in the xml file that I processed with this file and the one I posted in the previous post is a <dataroot> </dataroot> wrapper that MS Access 2003 puts around all of its xml exports.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple input xml / get data from other xml file elayaraja.s XSLT 3 July 25th, 2008 06:59 AM
VB.net, adding XML data to an existing XML file saikoboarder XML 11 April 17th, 2008 04:19 PM
Problem importing XML data to Excel bluerattle XML 2 July 3rd, 2007 03:35 PM
Problem comparing xml data vb6 method Pro VB 6 5 May 9th, 2007 11:19 AM
Filtering XML data based on differnt XML ahmed123 XSLT 5 August 11th, 2006 09:15 AM





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