Solved

Sort and Copy XML to new file XSL

Posted on 2012-03-12
10
367 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

18 Experts available now in Live!

Get 1:1 Help Now