Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 15th, 2011, 02:11 PM
Registered User
 
Join Date: Jun 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Transforming dynamic XML elements...

I need to process about 40 different XML files.
They are different in that the elements are unique to each file.

They are created as a result of analysis between a baseline and a test result.
If there are differences, a 'Differences' XML file is created. I have complete control of the content (headings and content) of these 'Differences files.
I would like to present this data to a webpage.

My problem is getting the stylesheet to gather the element names (and it's associated data) and presenting it in a table fashion.

I will not embarrass myself with showing my failed stylesheet attempts.
I would just like to ask how I can dynamically gather the node names and it's data and present it.

Below is an example of one XML file that I need to process.....

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="diffstyle.xsl"?>
<Differences xmlns="http://tempuri.org/Errors.xsd">
<Record_Type xmlns="XMLSchema">1</Record_Type><Function_Type xmlns="XMLSchema">2</Function_Type><In_Config xmlns="XMLSchema">1</In_Config><Chime_Type xmlns="XMLSchema">1</Chime_Type><Chime_Count xmlns="XMLSchema">255</Chime_Count><MCL_Color xmlns="XMLSchema">8</MCL_Color>
<Record_Type xmlns="XMLSchema">1</Record_Type><Function_Type xmlns="XMLSchema">3</Function_Type><In_Config xmlns="XMLSchema">1</In_Config><Chime_Type xmlns="XMLSchema">1</Chime_Type><Chime_Count xmlns="XMLSchema">256</Chime_Count><MCL_Color xmlns="XMLSchema">8</MCL_Color>
<Record_Type xmlns="XMLSchema">1</Record_Type><Function_Type xmlns="XMLSchema">25</Function_Type><In_Config xmlns="XMLSchema">1</In_Config><Chime_Type xmlns="XMLSchema">1</Chime_Type><Chime_Count xmlns="XMLSchema">1</Chime_Count><MCL_Color xmlns="XMLSchema">7</MCL_Color>
<Record_Type xmlns="XMLSchema">1</Record_Type><Function_Type xmlns="XMLSchema">26</Function_Type><In_Config xmlns="XMLSchema">1</In_Config><Chime_Type xmlns="XMLSchema">1</Chime_Type><Chime_Count xmlns="XMLSchema">1</Chime_Count><MCL_Color xmlns="XMLSchema">5</MCL_Color>
</Differences>

I hope I have the correct nomenclature. I'm assuming that in this case Record_Type, Function_Type etc are elements. If I am incorrect, please educate me.

Any help would be greatly appreciated. I have given it the college try and alas have come up short. Very short.

Last edited by brianthegood; June 15th, 2011 at 03:07 PM.. Reason: Incorrect nomenclature
 
Old June 15th, 2011, 05:49 PM
Registered User
 
Join Date: Jun 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default stylesheet used

So I cobbled together a style sheet based on info gleaned from this site

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:cdc="http://tempuri.org/CDCWorking.xsd">
<xsl:output method="text"/>
<xsl:template match="/Differences">
<html>
<body>
<h2>Test Results</h2>
<table border="1">
<xsl:value-of select="position()"/><xsl:text>: </xsl:text>
<xsl:value-of select="name()"/><xsl:text>#xa;</xsl:text>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="/Differences">
<xsl:apply-templates select="*"/>
</xsl:template>
</xsl:stylesheet>

All I get is the data values compressed together, no tags...

12112558
13112568
1251117
1261115

I would like a top row with tags with the data below it.

What am I missing???
 
Old June 15th, 2011, 06:30 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Google for "XSLT default namespace". Your "Differences" element is in a namespace, whereas match="Differences" will only match an element in no namespace. You need to make it match="x:Differences" where the stylesheet declares xmlns:x="http://tempuri.org/Errors.xsd".

I can't help you any more because you've given very little indication of the processing you want to perform. However, I'm slightly concerned that you should be trying to write XSLT code when you aren't quite sure what elements are. It's a a bit like starting to build a house when you're not quite sure what a brick is.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old June 15th, 2011, 09:33 PM
Registered User
 
Join Date: Jun 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the clarification.

Please appreciate the fact that I was tossed into the deep end of the pool late last week and then my cheeky boss handed me a 50 lb anvil to hang onto.
The pool - XML. The anvil - XSLT

Until this week I couldn't tell you what a stylesheet was.
Mea culpa if I've used the incorrect terms for fields as such.

The output you see in the first thread is the test output from a Powershell test bench driving test data (CSV) through a C# application with XML as the output.

I require the fields of the output (the text between the the angle brackets) to appear as the top line of a table and the data below it, as follows.

Record_Type Function_Type In_Config Chime_Type Chime_Count MCL_Color
1..........................2....................1. ............1...................255............... ..8
1..........................3....................1. ............1...................256............... ..8
1.........................25...................1.. ...........1.....................1................ ...7
1.........................26...................1.. ...........1.....................1................ ...5

The fields (top line) vary in number and text according to the XML file. That is why I require a dynamic way of picking off those fields and their associated data.

Could you throw a buddy a lifeline?
 
Old June 16th, 2011, 04:05 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If I understand you correctly, the structure of the input is

<diffs>
<a/>
<b/>
<c/>
<d/>
<a/>
<b/>
<c/>
<d/>
</diffs>

where the element names of a,b,c,d can be anything, and we somehow need to arrange them in groups. Why the person generating this XML couldn't be troubled to insert begin/end tags around each group defeats me, but I guess its the usual story that they were prepared to "leave this as an exercise for the reader", i.e. you.

if you're absolutely confident that the pattern is regular and there is more than one row, then you can calculate the size of the group as being the position of the first element whose name matches the name of the first element, if you get my meaning: in XSLT 2.0 that's

Code:
<xsl:variable name="group-size" select="index-of(*/node-name(.), *[1]/node-name(.))[2]"/>
Having computed the group-size, you then have the standard task of arranging the data in fixed-size groups:

Code:
<xsl:for-each select="*[position() mod $group-size] = 1">
   <tr>
      <xsl:for-each select="(.|following-sibling)[position() &lt;= $group-size()]">
          <td><xsl:value-of select="."/></td>
       </
    </
</
and then all that's left is adding the table headings:

Code:
<xsl:for-each select="*[position() &lt;= $group-size]">
   <th><xsl:value-of select="local-name()"/></th>
</xsl:for-each>
If you want to output CSV rather than an HTML table the principles are the same but you need to do some mucking about to escape special characters in the text (commas and quotes).
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference

Last edited by mhkay; June 16th, 2011 at 04:08 AM.. Reason: typo





Similar Threads
Thread Thread Starter Forum Replies Last Post
Transforming XML From One Namespace to Another mail4kaja XSLT 1 November 10th, 2008 10:14 AM
Transforming XML to XML using XSL sakreck XSLT 0 January 9th, 2007 11:48 AM
transforming a XML on the basis of another XML Diex75 XSLT 4 November 9th, 2006 09:11 AM
problem transforming XML using xslt micky3248 XSLT 7 August 18th, 2006 03:52 AM
Please help, xml transforming using xslt !!! daula7 XSLT 0 May 11th, 2006 01:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.