Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

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
0
Nigel_Taylor
Asked:
Nigel_Taylor
  • 6
  • 4
1 Solution
 
Geert BormansCommented:
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
 
Geert BormansCommented:
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
 
Nigel_TaylorAuthor Commented:
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
Independent Software Vendors: 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!

 
Geert BormansCommented:
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
 
Nigel_TaylorAuthor Commented:
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
 
Geert BormansCommented:
welcome
0
 
Nigel_TaylorAuthor Commented:
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
 
Geert BormansCommented:
in the template for data
<xsl:template match="Data">
you could do
<xsl:if test="not(*)">Report empty Data</xsl:if>
0
 
Nigel_TaylorAuthor Commented:
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
 
Geert BormansCommented:
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now