Solved

Help with XSL file - sorting and attributes

Posted on 2011-03-04
8
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 

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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

752 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