Avatar of Nigel_Taylor
Nigel_Taylor
 asked on

Sort and Copy XML to new file XSL

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
XML

Avatar of undefined
Last Comment
Gertone (Geert Bormans)

8/22/2022 - Mon
Gertone (Geert Bormans)

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?
ASKER CERTIFIED SOLUTION
Gertone (Geert Bormans)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Nigel_Taylor

ASKER
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
Gertone (Geert Bormans)

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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Nigel_Taylor

ASKER
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
Gertone (Geert Bormans)

welcome
Nigel_Taylor

ASKER
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Gertone (Geert Bormans)

in the template for data
<xsl:template match="Data">
you could do
<xsl:if test="not(*)">Report empty Data</xsl:if>
Nigel_Taylor

ASKER
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

Gertone (Geert Bormans)

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy