Solved

Help with XSL file - sorting and attributes

Posted on 2011-03-04
8
323 Views
Last Modified: 2013-11-18
I've fiddled with this for some time and can't get the output formatted how I want from this sample .xml file.

<file>
<zid>127</zid>
<path>/</path>
<inheritedRightsFilter value="rwceafms"><read/><write/><create/><erase/><accessControl/><fileScan/><modify/><supervisor/></inheritedRightsFilter>
<trustee><name>.O=ABC.T=ABCTREE.</name><id>52ec6e00-7f3c-11d6-ab-54-0000e26c189c</id><rights value="rf"><read/><fileScan/></rights></trustee>
<trustee><name>.OU=workstations.OU=Perth.O=ABC.T=ABCTREE.</name><id>d2ca2514-f754-43dc-fc-98-1425cad254f7</id><rights value="rwcefm"><read/><write/><create/><erase/><fileScan/><modify/></rights></trustee>
<trustee><name>.CN=admin.O=ABC.T=ABCTREE.</name><id>52ec6e02-7f3c-11d6-ab-54-0000e26c189c</id><rights value="rwceafm"><read/><write/><create/><erase/><accessControl/><fileScan/><modify/></rights></trustee>
</file>

<file>
<zid>129</zid><path>/._NETWARE</path>
<inheritedRightsFilter value="s"><supervisor/></inheritedRightsFilter>
</file>

<file>
<zid>131</zid>
<path>/it</path>
<inheritedRightsFilter value="s"><supervisor/></inheritedRightsFilter>
<trustee><name>.CN=admin.O=ABC.T=ABCTREE.</name><id>52ec6e02-7f3c-11d6-ab-54-0000e26c189c</id><rights value="rwceafm"><read/><write/><create/><erase/><accessControl/><fileScan/><modify/></rights></trustee>
<trustee><name>.CN=adminbak.O=ABC.T=ABCTREE.</name><id>851e2b07-40f5-4a3a-8c-9c-072b1e85f540</id><rights value="rwceafm"><read/><write/><create/><erase/><accessControl/><fileScan/><modify/></rights></trustee>
<trustee><name>.CN=blokeman.OU=Perth.O=ABC.T=ABCTREE.</name><id>95d06080-814b-11d6-ab-56-0000e26c189c</id><rights value="rf"><read/><fileScan/></rights></trustee>
</file>

Open in new window


What I want is to extract a subset of the information as follows:
list sorted by <path>
   <inheritedRightsFilter value> attribute <!-- always just one entry -->
   List sorted by <trustee>
       <name>  <rights value> attribute

The problem I am having is sorting the <path> list and creating the sorted sublist <trustee>, plus extracting the 'value' attribute from <inheritedRightsFilter>.
What is the easiest way to achieve this?
0
Comment
Question by:blokeman
  • 4
  • 4
8 Comments
 
LVL 18

Assisted Solution

by:zc2
zc2 earned 500 total points
ID: 35041506
Can not say I quite understood the desired output format, but may be my sample will give you some ideas...
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes" encoding="iso-8859-1"/>
	<xsl:template match="/root">
	
		<xsl:for-each select="file">
			<xsl:sort select="path" order="ascending"/>
			
			path: <xsl:value-of select="path"/>
			inheritedRightsFilter value attribute: <xsl:value-of select="inheritedRightsFilter/@value"/>

			<xsl:for-each select="trustee">
				<xsl:sort select="name" order="ascending"/>
				trustee: <xsl:value-of select="name"/> 
				rights value attribute: <xsl:value-of select="rights/@value"/>,
			</xsl:for-each>
  		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 18

Expert Comment

by:zc2
ID: 35041515
I forgot to mention, the XML you provided is not well-formed, it does not grow from a single root. I added a node called "root" and referenced it from the XSLT (<xsl:template match="/root">). In your real solution this approach could be different.
0
 

Author Comment

by:blokeman
ID: 35041541
Wow - that was quick!
I'll test your advice now...

Sorry about my original file not being well-formed.  Here is the full file:

<?xml version="1.0" encoding="UFT-8" ?>
<?xml-stylesheet type="text/xsl" href="trustee-apps.xsl"?>
<metadata>
<trusteeInfo>
<file>
<zid>127</zid>
<path>/</path>
<inheritedRightsFilter value="rwceafms"><read/><write/><create/><erase/><accessControl/><fileScan/><modify/><supervisor/></inheritedRightsFilter>
<trustee><name>.O=ABC.T=ABCTREE.</name><id>52ec6e00-7f3c-11d6-ab-54-0000e26c189c</id><rights value="rf"><read/><fileScan/></rights></trustee>
<trustee><name>.OU=workstations.OU=Perth.O=ABC.T=ABCTREE.</name><id>d2ca2514-f754-43dc-fc-98-1425cad254f7</id><rights value="rwcefm"><read/><write/><create/><erase/><fileScan/><modify/></rights></trustee>
<trustee><name>.CN=admin.O=ABC.T=ABCTREE.</name><id>52ec6e02-7f3c-11d6-ab-54-0000e26c189c</id><rights value="rwceafm"><read/><write/><create/><erase/><accessControl/><fileScan/><modify/></rights></trustee>
</file>

<file>
<zid>129</zid><path>/._NETWARE</path>
<inheritedRightsFilter value="s"><supervisor/></inheritedRightsFilter>
</file>

<file>
<zid>131</zid>
<path>/it</path>
<inheritedRightsFilter value="s"><supervisor/></inheritedRightsFilter>
<trustee><name>.CN=admin.O=ABC.T=ABCTREE.</name><id>52ec6e02-7f3c-11d6-ab-54-0000e26c189c</id><rights value="rwceafm"><read/><write/><create/><erase/><accessControl/><fileScan/><modify/></rights></trustee>
<trustee><name>.CN=adminbak.O=ABC.T=ABCTREE.</name><id>851e2b07-40f5-4a3a-8c-9c-072b1e85f540</id><rights value="rwceafm"><read/><write/><create/><erase/><accessControl/><fileScan/><modify/></rights></trustee>
<trustee><name>.CN=blokeman.OU=Perth.O=ABC.T=ABCTREE.</name><id>95d06080-814b-11d6-ab-56-0000e26c189c</id><rights value="rf"><read/><fileScan/></rights></trustee>
</file>
</trusteeInfo>
</metadata>

Open in new window

0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

Author Comment

by:blokeman
ID: 35041618
Very close! :-)
Just needs a bit of tweaking...

After modifying your version slightly, see my line 4, this is the .xsl file I used:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes" encoding="iso-8859-1"/>
	<xsl:template match="/trusteeInfo">
	
		<xsl:for-each select="file">
			<xsl:sort select="path" order="ascending"/>
			
			path: <xsl:value-of select="path"/>
			inheritedRightsFilter value attribute: <xsl:value-of select="inheritedRightsFilter/@value"/>

			<xsl:for-each select="trustee">
				<xsl:sort select="name" order="ascending"/>
				trustee: <xsl:value-of select="name"/> 
				rights value attribute: <xsl:value-of select="rights/@value"/>,
			</xsl:for-each>
  		</xsl:for-each>

	</xsl:template>
</xsl:stylesheet>

Open in new window


and this is the result:


127/

.O=ABC.T=ABCTREE.52ec6e00-7f3c-11d6-ab-54-0000e26c189c
.OU=workstations.OU=Perth.O=ABC.T=ABCTREE.d2ca2514-f754-43dc-fc-98-1425cad254f7
.CN=admin.O=ABC.T=ABCTREE.52ec6e02-7f3c-11d6-ab-54-0000e26c189c


129/._NETWARE


131/it

.CN=admin.O=ABC.T=ABCTREE.52ec6e02-7f3c-11d6-ab-54-0000e26c189c
.CN=adminbak.O=ABC.T=ABCTREE.851e2b07-40f5-4a3a-8c-9c-072b1e85f540
.CN=blokeman.OU=Perth.O=ABC.T=ABCTREE.95d06080-814b-11d6-ab-56-0000e26c189c

Open in new window


The result I am after is, if we use for example the /it folder (the last output section) :
 /it
   inheritedRightsFilter value attribute: s
   trustee: .CN=admin.O=ABC.T=ABCTREE.                        rights value attribute: rwceafm
   trustee: .CN=adminbak.O=ABC.T=ABCTREE.               rights value attribute: rwceafm
   trustee: .CN=blokeman.OU=Perth.O=ABC.T=ABCTREE.  rights value attribute: rf
0
 

Author Comment

by:blokeman
ID: 35041771
Any ideas why this line didn't output?
inheritedRightsFilter value attribute: <xsl:value-of select="inheritedRightsFilter/@value"/>
0
 
LVL 18

Accepted Solution

by:
zc2 earned 500 total points
ID: 35044198
It strange in your output each value is placed on a new line. May be that because you're using different XSLT processor (I use MSXML). But, anyway, this is not important, because it's the totally wrong output.
The matter of fact, the XSLT template I sent you and you have modified is not used at all. That because the output is completely differ from the desired.
And the template is not used, because the XML has two enclosed root nodes (the <metadata> and the       <trusteeInfo> ), but the XSLT has only one (/trusteeInfo) mentioned in the template pattern: <xsl:template match="/trusteeInfo">.

Please try to change the row (the one you modified) to the following:
<xsl:template match="/metadata/trusteeInfo">

0
 

Author Comment

by:blokeman
ID: 35066917
Thanks!
This looks great
	PATH: /
			INHERITED RIGHTS FILTER : rwceafms
				TRUSTEE: .CN=admin.O=ABC.T=ABCTREE. 
				RIGHTS: rwceafm
				TRUSTEE: .O=ABC.T=ABCTREE. 
				RIGHTS: rf
				TRUSTEE: .OU=workstations.OU=Perth.O=ABC.T=ABCTREE. 
				RIGHTS: rwcefm
			
			PATH: /it
			INHERITED RIGHTS FILTER : s
				TRUSTEE: .CN=adminbak.O=ABC.T=ABCTREE. 
				RIGHTS: rwceafm
				TRUSTEE: .CN=admin.O=ABC.T=ABCTREE. 
				RIGHTS: rwceafm
				TRUSTEE: .CN=blokeman.OU=Perth.O=ABC.T=ABCTREE. 
				RIGHTS: rf
			
			PATH: /._NETWARE
			INHERITED RIGHTS FILTER : s

Open in new window


I'll tweak it a bit more to use a two column table rather than the list, for TRUSTEE and RIGHTS.  This should help readability.
0
 
LVL 18

Expert Comment

by:zc2
ID: 35070786
You're welcome!
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 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