View Single Post
  #1 (permalink)  
Old January 14th, 2011, 11:39 AM
inieto inieto is offline
Registered User
 
Join Date: Jan 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Question How to sort a XML file itself

Hi folks,

I have a close project where we don't have the source code but a back office where we can perform some sort of operations. All these operations can be exported as an XML file. The problem is that the export method follows a hierarchy but not an order. Therefore we want to sort it in order to be able to do diffs and that way share our work.

I've taken an example from http://bytes.com/topic/net/answers/8...y-printed-diff and with some help from Martin, I've got to this XSL.

Code:
<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="@* | text() | comment() | processing-instruction()">
   <xsl:copy/>
 </xsl:template>


 <xsl:template match="*[*]">
   <xsl:copy>
     <xsl:apply-templates select="@*"/>
     <xsl:apply-templates select="*">
       <xsl:sort select="local-name()" data-type="text"/>
       <xsl:sort select="@id" data-type="text"/>
       <xsl:sort select="@name" data-type="text"/>
     </xsl:apply-templates>
   </xsl:copy>
 </xsl:template>

 <xsl:template match="*[not(*)]">

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

</xsl:stylesheet>
I'll paste on the bottom some sample input, but first I'll describe what is missing:

There's a tag called "pagelet-component" which has a collection of slots and inside a tag name slot which has to be ordered by it's attribute tag definition-name.
There's another tag called "pagelet-pagelet-entry-pont-assignment" which has pagelet(s) and pagelet-entry-point(s) tabs which have to be ordered by it's attribute id.
I've tried adding some new sort criterias, but it didn't work
Code:
<xsl:sort select="pagelet-component/slots/slot/definition-name" data-type="text"/>
<xsl:sort select="pagelet-pagelet-entry-point-assignment/pagelet/@id" data-type="text"/>
<xsl:sort select="pagelet-pagelet-entry-point-assignment/pagelet-entry-point/@id" data-type="text"/>
Well, and below is the sample input data:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<input
xmlns="http://www.x.com/xml/ns/xXx/6.5/bc_pmc/impex"
xmlns:dt="http://www.x.com/xml/ns/xXx/6.5/core/impex-dt"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.x.com/xml/ns/xXx/6.5/bc_pmc/impex bc_pmc.xsd">
<pagelet-entry-point id="systempage.productComparision.pagelet2-systempage.productComparision">
<definition-name>systempage.productComparision.pagelet2-systempage.productComparision</definition-name>
<page>true</page>
<sorting>false</sorting>
<display-name xml:lang="en-US">Product Comparision</display-name>
<custom-attributes>
</custom-attributes>
</pagelet-entry-point>
<pagelet-pagelet-entry-point-assignment>
<pagelet-entry-point id="systempage.productComparision.pagelet2-systempage.productComparision2" domain="domain"/>
<pagelet id="pg_20100829_210958" domain="domain"/>
<position>2</position>
<valid-from>08/29/10 09:09:00 pm</valid-from>
<online>true</online>
</pagelet-pagelet-entry-point-assignment>
<pagelet-entry-point id="systempage.productComparision.pagelet2-systempage.productComparision">
<definition-name>systempage.productComparision.pagelet2-systempage.productComparision</definition-name>
<page>true</page>
<sorting>false</sorting>
<display-name xml:lang="en-US">Product Comparision</display-name>
<custom-attributes>
</custom-attributes>
</pagelet-entry-point>
<pagelet-pagelet-entry-point-assignment>
<pagelet-entry-point id="systempage.productComparision.pagelet2-systempage.productComparision" domain="domain"/>
<pagelet id="pg_20100829_210957" domain="domain"/>
<position>1</position>
<valid-from>08/29/10 09:09:00 pm</valid-from>
<online>true</online>
</pagelet-pagelet-entry-point-assignment>
<pagelet-page id="pg_20100829_210957">
<definition-name>pagevariation.productComparision.pagelet2-pagevariation.productComparision</definition-name>
<display-name xml:lang="en-US">Default Product Comparision</display-name>
<description xml:lang="en-US"></description>
<valid-from>08/29/10 09:09:00 pm</valid-from>
<online>true</online>
<author business-partner-no="admin" domain="domain"/>
<configuration-parameters>
<configuration-parameter name="metaInfo">
<definition-name>pagevariation.productComparision.pagelet2-pagevariation.productComparision-metaInfo</definition-name>
</configuration-parameter>
</configuration-parameters>
<slots>
<slot>
<definition-name>slots.main.pagelet2-slotContent</definition-name>
<optional>false</optional>
<sorting>false</sorting>
</slot>
</slots>
<custom-attributes>
</custom-attributes>
</pagelet-page>
<pagelet-slot-assignment id="tcPAqOKBrusAAAEtEwIaBHrN">
<slot definition-name="slots.main.pagelet2-slotContent" parent-pagelet-id="pg_20100829_210957" domain="domain"/>
<pagelet id="cmp_20100822_182608" domain="domain"/>
<position>3</position>
<valid-from>08/29/10 09:10:00 pm</valid-from>
<online>true</online>
</pagelet-slot-assignment>
<pagelet-component id="cmp_20100822_182608">
<definition-name>comp.footer.section.pagelet2-comp.footer.section</definition-name>
<display-name xml:lang="en-US">Footer Container</display-name>
<valid-from>08/22/10 06:26:00 pm</valid-from>
<online>true</online>
<author business-partner-no="admin" domain="domain"/>
<configuration-parameters>
<configuration-parameter name="cssClass">
<definition-name>comp.footer.section.pagelet2-comp.footer.section-cssClass</definition-name>
</configuration-parameter>
</configuration-parameters>
<slots>
<slot>
<definition-name>comp.footer.section.pagelet2-slotFooterSection</definition-name>
<optional>false</optional>
<sorting>false</sorting>
</slot>
</slots>
<custom-attributes>
</custom-attributes>
</pagelet-component>
<pagelet-slot-assignment id="vbnAqOKB3WcAAAEt7QIaBHrN">
<slot definition-name="comp.footer.section.pagelet2-slotFooterSection" parent-pagelet-id="cmp_20100822_182608" domain="domain"/>
<pagelet id="cmp_20100812_172330" domain="domain"/>
<position>1</position>
<valid-from>08/22/10 06:26:00 pm</valid-from>
<online>true</online>
</pagelet-slot-assignment>
<pagelet-component id="cmp_20100812_172330">
<definition-name>comp.common.group.pagelet2-comp.common.group</definition-name>
<display-name xml:lang="en-US">Footer Component Group</display-name>
<valid-from>08/12/10 05:23:00 pm</valid-from>
<online>true</online>
<author business-partner-no="admin" domain="domain"/>
<configuration-parameters>
<configuration-parameter name="grid">
<definition-name>comp.common.group.pagelet2-comp.common.group-grid</definition-name>
<value name="grid">width=24;prefix=0;suffix=0;alpha=0;omega=0;push=0;pull=0</value>
</configuration-parameter>
<configuration-parameter name="customHoverClass">
<definition-name>comp.common.group.pagelet2-comp.common.group-customHoverClass</definition-name>
</configuration-parameter>
<configuration-parameter name="cssClass">
<definition-name>comp.common.group.pagelet2-comp.common.group-cssClass</definition-name>
</configuration-parameter>
<configuration-parameter name="hoverState">
<definition-name>comp.common.group.pagelet2-comp.common.group-hoverState</definition-name>
</configuration-parameter>
</configuration-parameters>
<slots>
<slot>
<definition-name>comp.common.group.pagelet2-slotContentGroup</definition-name>
<optional>false</optional>
<sorting>false</sorting>
</slot>
</slots>
<custom-attributes>
</custom-attributes>
</pagelet-component>
<pagelet-slot-assignment id="aUXAqOKBvwMAAAEtuQMaBHrN">
<slot definition-name="comp.common.group.pagelet2-slotContentGroup" parent-pagelet-id="cmp_20100812_172330" domain="domain"/>
<pagelet id="cmp_20101119_122618" domain="domain"/>
<position>5</position>
<valid-from>11/19/10 12:26:00 pm</valid-from>
<online>true</online>
</pagelet-slot-assignment>
<pagelet-component id="cmp_20101119_122618">
<definition-name>comp.navigation.linkCollection.pagelet2-comp.navigation.linkCollection</definition-name>
<display-name xml:lang="en-US">Social Media Links</display-name>
<description xml:lang="en-US"></description>
<valid-from>11/19/10 12:26:00 pm</valid-from>
<online>true</online>
<author business-partner-no="admin" domain="domain"/>
<configuration-parameters>
<configuration-parameter name="cssClass">
<definition-name>comp.navigation.linkCollection.pagelet2-comp.navigation.linkCollection-cssClass</definition-name>
<value name="cssClass">ws-social-media-links</value>
</configuration-parameter>
<configuration-parameter name="cssId">
<definition-name>comp.navigation.linkCollection.pagelet2-comp.navigation.linkCollection-cssId</definition-name>
</configuration-parameter>
</configuration-parameters>
<slots>
<slot>
<definition-name>comp.navigation.linkCollection.pagelet2-slotLinkCollection</definition-name>
<optional>false</optional>
<sorting>false</sorting>
</slot>
</slots>
<custom-attributes>
</custom-attributes>
</pagelet-component>
<pagelet-slot-assignment id="ydTAqOKBAf0AAAEtkwQaBHrN">
<slot definition-name="comp.navigation.linkCollection.pagelet2-slotLinkCollection" parent-pagelet-id="cmp_20101119_122618" domain="domain"/>
<pagelet id="cmp_20101119_122646" domain="domain"/>
<position>1</position>
<valid-from>11/19/10 12:26:00 pm</valid-from>
<online>true</online>
</pagelet-slot-assignment>
<pagelet-component id="cmp_20101119_122646">
<definition-name>comp.navigation.simpleLink.pagelet2-comp.navigation.simpleLink</definition-name>
<display-name xml:lang="en-US">Facebook Link</display-name>
<description xml:lang="en-US"></description>
<valid-from>11/19/10 12:26:00 pm</valid-from>
<online>true</online>
<author business-partner-no="admin" domain="domain"/>
<configuration-parameters>
<configuration-parameter name="title">
<definition-name>comp.navigation.simpleLink.pagelet2-comp.navigation.simpleLink-title</definition-name>
<value name="title" xml:lang="en-US">Facebook</value>
</configuration-parameter>
<configuration-parameter name="link">
<definition-name>comp.navigation.simpleLink.pagelet2-comp.navigation.simpleLink-link</definition-name>
<value name="link">http://www.facebook.com</value>
</configuration-parameter>
<configuration-parameter name="cssClass">
<definition-name>comp.navigation.simpleLink.pagelet2-comp.navigation.simpleLink-cssClass</definition-name>
<value name="cssClass" xml:lang="en-US">ws-facebook</value>
</configuration-parameter>
<configuration-parameter name="displayname">
<definition-name>comp.navigation.simpleLink.pagelet2-comp.navigation.simpleLink-displayname</definition-name>
<value name="displayname" xml:lang="en-US">Facebook</value>
</configuration-parameter>
</configuration-parameters>
<custom-attributes>
</custom-attributes>
</pagelet-component>
<pagelet-slot-assignment id="jL_AqOKB4f4AAAEtOQYaBHrN">
<slot definition-name="comp.navigation.linkCollection.pagelet2-slotLinkCollection" parent-pagelet-id="cmp_20101119_122618" domain="domain"/>
<pagelet id="cmp_20101119_123015" domain="domain"/>
<position>3</position>
<valid-from>11/19/10 12:30:00 pm</valid-from>
<online>true</online>
</pagelet-slot-assignment>
</input>
Any help is appreciated :)
Ignacio
Reply With Quote