Solved

Sort and Copy XML to new file XSL

Posted on 2012-03-12
10
373 Views
Last Modified: 2012-03-19
Hi Guys,

I have an xml file where the data is as below and basically I need to copy all the information in the consignment section and to copy all the trackingevents to a new XML file but sorting the tracking events by date / time order.

<Data>
 <HeaderData>Text<HeaderData>
 <consignment>
    <consignmentno>1234</consignmentno>
    ..... Other Nodes
  </consignment>
  <tracking>
    <date>2012-03-10</date>
    <time>13:56</time>
    <status>Depot</status>
  <tracking>
  <tracking>
    <date>2012-03-09</date>
    <time>13:56</time>
    <status>At Warehouse</status>
  <tracking>
  <tracking>
    <date>2012-03-12</date>
    <time>13:56</time>
    <status>Delivered</status>
  <tracking>
</Data>

Open in new window


Here is what I have so far but it just returns the whole file!!

<xsl:template match="@*|node()">
   <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
</xsl:template>


<xsl:template match="Data/consignment">
                <consignment>
	<xsl:apply-templates select="*" />  
                </consignment>
</xsl:template>

Open in new window


Any help with be greatfully appreciated.

Regards,

Nigel
0
Comment
Question by:Nigel_Taylor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 37713846
Hi Nigel,

it is very unclear what you want to do.

Your XSLT copies all the nodes by default
and soes something similar with the consignment elementso yes, you get an identical copy of the input to the output

Is that what you want?
Exactly the same output, but with the tracking sorted?
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 135 total points
ID: 37713865
Here is the XSLT you would need in that case

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Data">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="HeaderData | consignment"/>
            <xsl:apply-templates select="tracking">
                <xsl:sort select="date"/>
                <xsl:sort select="time"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:Nigel_Taylor
ID: 37714031
Hi Gertone,

Thanks for helping Gertone. It was a long day yesturday and I could have been more descriptive.

Yes basically from the whole file I just want to return everything within the consignment and trackingevents nodes and doing the sort on the tracking events.

I have copied your code from above and altered the line

<xsl:apply-templates select="HeaderData | consignment"/>

Open in new window


so that it just reads

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

Open in new window


and in ASP I am getting the error

msxml3.dll (0x80004005)
The stylesheet does not contain a document element. The stylesheet may be empty, or it may not be a well-formed XML document.

I have checked all the tags open and close where appropriate and speach marks in the right place. Any thoughts on where to look next?

Regards,

Nigel
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 37714161
The XSLT as I posted it was tested against your data prior to posting,
and the line you changed did not add syntactical errors.

I assume that the error message indicates that the XSLT was not loaded correctly in the ASP.
I would start there trying to get it working
0
 

Author Closing Comment

by:Nigel_Taylor
ID: 37735320
Thanks. Sorry for the delay in completing this. Some where along the line I had inserted a blank line at the top of the style sheet causing the error.

Thanks Again.

Nigel
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 37735326
welcome
0
 

Author Comment

by:Nigel_Taylor
ID: 37738178
Sorry just a quick question.

As this is data coming back from a webservice. If all I get back is <Data /> how do I test for this?

Nigel
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 37738344
in the template for data
<xsl:template match="Data">
you could do
<xsl:if test="not(*)">Report empty Data</xsl:if>
0
 

Author Comment

by:Nigel_Taylor
ID: 37738706
Thanks Gertone.

I have this in the tempalte match but when I run it i dont get "Report Empty Data" returned.

	
<xsl:template match="/Data">

		<xsl:if test="not(*)">
			<p>Report empty Data</p>
			<hr />
		</xsl:if>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 37738889
Hi Nigel,

There might be a template conflict with the match attribute

you already have a template
    <xsl:template match="Data">
which I hope is active

It could be that Data is not the root of your XML source
or taht two "Data" templates are conflicting
0

Featured Post

A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question