Solved

Sort and Copy XML to new file XSL

Posted on 2012-03-12
10
368 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
  • 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SVG, D3, scaling, translation 11 141
Issue exporting to XML with Excel 2016 3 79
VB.NET and XML parsing 6 65
Create html table using xsl 8 28
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now