Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

Sorting XML -> XML using XSL

$
0
0

I'm new to XSL and having difficulty in understanding how/when patterns match/process. I understand that it is declarative, not procedural, and am at the disadvantage of trying to process XML -> XML, not XML -> HTML (which is what all the tutorials appear to show). In essence I want to sort a subtree of my XML file (which is a Visual studio document - annoying it doesn't sort the files when added which is really bugging my team). In the subtree I want to sort the sibilngs by an attribute. A truncated sample of the file is:

<?xml version="1.0"?><SqlWorkbenchSqlProject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="PMISQL"><Items><LogicalFolder Name="Connections" Type="2" Sorted="true"><Items><ConnectionNode Name="cerberus:XPOOLE\alistair.moreton"><Created>2013-06-28T15:50:27.919788+01:00</Created><Type>SQL</Type><Server>cerberus</Server><UserName /><Authentication>Windows Authentication</Authentication><InitialDB>master</InitialDB><LoginTimeout>15</LoginTimeout><ExecutionTimeout>0</ExecutionTimeout><ConnectionProtocol>NotSpecified</ConnectionProtocol><ApplicationName>Microsoft SQL Server Management Studio - Query</ApplicationName></ConnectionNode><ConnectionNode Name="hector:XPOOLE\alistair.moreton"><Created>2013-07-02T15:27:02.5793203+01:00</Created><Type>SQL</Type><Server>hector</Server><UserName /><Authentication>Windows Authentication</Authentication><InitialDB /><LoginTimeout>15</LoginTimeout><ExecutionTimeout>0</ExecutionTimeout><ConnectionProtocol>NotSpecified</ConnectionProtocol><ApplicationName>Microsoft SQL Server Management Studio - Query</ApplicationName></ConnectionNode></Items></LogicalFolder><LogicalFolder Name="Queries" Type="0" Sorted="true"><Items><FileNode Name="PMI001_AddressHistory.sql"><AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:cerberus:True</AssociatedConnectionMoniker><AssociatedConnSrvName>cerberus</AssociatedConnSrvName><AssociatedConnUserName /><FullPath>PMI001_AddressHistory.sql</FullPath></FileNode><FileNode Name="PMI000_DataWarehouseState.sql"><AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:hector:True</AssociatedConnectionMoniker><AssociatedConnSrvName>hector</AssociatedConnSrvName><AssociatedConnUserName /><FullPath>PMI000_DataWarehouseState.sql</FullPath></FileNode><FileNode Name="PMI003_PlannedAdmissions.sql"><AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:cerberus:True</AssociatedConnectionMoniker><AssociatedConnSrvName>cerberus</AssociatedConnSrvName><AssociatedConnUserName /><FullPath>PMI003_PlannedAdmissions.sql</FullPath></FileNode><FileNode Name="PMI004_AllActivity.sql"><AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:cerberus:True</AssociatedConnectionMoniker><AssociatedConnSrvName>cerberus</AssociatedConnSrvName><AssociatedConnUserName /><FullPath>PMI004_AllActivity.sql</FullPath></FileNode><FileNode Name="PMI002_ClinicCodes.sql"><AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:cerberus:True</AssociatedConnectionMoniker><AssociatedConnSrvName>cerberus</AssociatedConnSrvName><AssociatedConnUserName /><FullPath>PMI002_ClinicCodes.sql</FullPath></FileNode></Items></LogicalFolder><LogicalFolder Name="Miscellaneous" Type="3" Sorted="true"><Items /></LogicalFolder></Items><SccProjectName>$/DataWarehouse/Dev/Reports/PMI</SccProjectName><SccAuxPath /><SccLocalPath>.</SccLocalPath><SccProvider>MSSCCI:Team Foundation Server MSSCCI Provider</SccProvider></SqlWorkbenchSqlProject>

and the XSL file I'm applying is:

<?xml version="1.0" encoding="iso-8859-1"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:template match="/"><xsl:copy-of select="."/><xsl:apply-templates/></xsl:template><xsl:template match="/SqlWorkbenchSqlProject/Items/LogicalFolder[Name='Connections']"><xsl:copy-of select="."/></xsl:template><xsl:template match="/SqlWorkbenchSqlProject/Items/LogicalFolder[Name='Queries']"><xsl:copy-of select="."/></xsl:template><xsl:template match="/SqlWorkbenchSqlProject/Items/LogicalFolder[Name='Queries']/Items"><xsl:for-each select="./FileNode"><xsl:sort select="@Name" data-type="text" case-order="upper-first"/><xsl:copy-of select="."/></xsl:for-each></xsl:template><xsl:template match="/SqlWorkbenchSqlProject/Items/LogicalFolder[Name='Miscellaneous']"><xsl:copy-of select="."/></xsl:template></xsl:stylesheet>

I want to sort the FileNode siblings in /SqlWorkbenchSqlProject/Items/LogicalFolder where Name="Queries" by their @Name attribute, but I want the rest of the file to be output as it is.

If I run the transform above, I get additional junk at the end of the file, i.e.:

<?xml version="1.0" encoding="utf-8"?><SqlWorkbenchSqlProject Name="PMISQL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Items><LogicalFolder Name="Connections" Type="2" Sorted="true"><Items><ConnectionNode Name="cerberus:XPOOLE\alistair.moreton"><Created>2013-06-28T15:50:27.919788+01:00</Created><Type>SQL</Type><Server>cerberus</Server><UserName /><Authentication>Windows Authentication</Authentication><InitialDB>master</InitialDB><LoginTimeout>15</LoginTimeout><ExecutionTimeout>0</ExecutionTimeout><ConnectionProtocol>NotSpecified</ConnectionProtocol><ApplicationName>Microsoft SQL Server Management Studio - Query</ApplicationName></ConnectionNode><ConnectionNode Name="hector:XPOOLE\alistair.moreton"><Created>2013-07-02T15:27:02.5793203+01:00</Created><Type>SQL</Type><Server>hector</Server><UserName /><Authentication>Windows Authentication</Authentication><InitialDB /><LoginTimeout>15</LoginTimeout><ExecutionTimeout>0</ExecutionTimeout><ConnectionProtocol>NotSpecified</ConnectionProtocol><ApplicationName>Microsoft SQL Server Management Studio - Query</ApplicationName></ConnectionNode></Items></LogicalFolder><LogicalFolder Name="Queries" Type="0" Sorted="true"><Items><FileNode Name="PMI001_AddressHistory.sql"><AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:cerberus:True</AssociatedConnectionMoniker><AssociatedConnSrvName>cerberus</AssociatedConnSrvName><AssociatedConnUserName /><FullPath>PMI001_AddressHistory.sql</FullPath></FileNode><FileNode Name="PMI000_DataWarehouseState.sql"><AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:hector:True</AssociatedConnectionMoniker><AssociatedConnSrvName>hector</AssociatedConnSrvName><AssociatedConnUserName /><FullPath>PMI000_DataWarehouseState.sql</FullPath></FileNode><FileNode Name="PMI003_PlannedAdmissions.sql"><AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:cerberus:True</AssociatedConnectionMoniker><AssociatedConnSrvName>cerberus</AssociatedConnSrvName><AssociatedConnUserName /><FullPath>PMI003_PlannedAdmissions.sql</FullPath></FileNode><FileNode Name="PMI004_AllActivity.sql"><AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:cerberus:True</AssociatedConnectionMoniker><AssociatedConnSrvName>cerberus</AssociatedConnSrvName><AssociatedConnUserName /><FullPath>PMI004_AllActivity.sql</FullPath></FileNode><FileNode Name="PMI002_ClinicCodes.sql"><AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:cerberus:True</AssociatedConnectionMoniker><AssociatedConnSrvName>cerberus</AssociatedConnSrvName><AssociatedConnUserName /><FullPath>PMI002_ClinicCodes.sql</FullPath></FileNode></Items></LogicalFolder><LogicalFolder Name="Miscellaneous" Type="3" Sorted="true"><Items /></LogicalFolder></Items><SccProjectName>$/DataWarehouse/Dev/Reports/PMI</SccProjectName><SccAuxPath /><SccLocalPath>.</SccLocalPath><SccProvider>MSSCCI:Team Foundation Server MSSCCI Provider</SccProvider></SqlWorkbenchSqlProject>




          2013-06-28T15:50:27.919788+01:00
          SQL
          cerberus

          Windows Authentication
          master
          15
          0
          NotSpecified
          Microsoft SQL Server Management Studio - Query


          2013-07-02T15:27:02.5793203+01:00
          SQL
          hector

          Windows Authentication

          15
          0
          NotSpecified
          Microsoft SQL Server Management Studio - Query






          8c91a03d-f9b4-46c0-a305-b5dcc79ff907:cerberus:True
          cerberus

          PMI001_AddressHistory.sql


          8c91a03d-f9b4-46c0-a305-b5dcc79ff907:hector:True
          hector

          PMI000_DataWarehouseState.sql


          8c91a03d-f9b4-46c0-a305-b5dcc79ff907:cerberus:True
          cerberus

          PMI003_PlannedAdmissions.sql


          8c91a03d-f9b4-46c0-a305-b5dcc79ff907:cerberus:True
          cerberus

          PMI004_AllActivity.sql


          8c91a03d-f9b4-46c0-a305-b5dcc79ff907:cerberus:True
          cerberus

          PMI002_ClinicCodes.sql







  $/DataWarehouse/Dev/Reports/PMI

  .
  MSSCCI:Team Foundation Server MSSCCI Provider

So:

(a) any idea how I modify the transform to achieve my aim and why, and

(b) where is the junk coming from? I have no XSL nodes that are xsl:value-of, so why are values being output for nodes that have already been processed, and lastly,

(c) in general, if I want to generate as output, the input file, except for a subtree that I want to treat differently, what is the pattern to do this?

Any help will be very much appreciated.

Many thanks,

Mark




Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>