473,804 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tricky XSL transformation

jl
Hi everybody!

I have an interesting problem - and so far, I haven't been able to
solve it. Maybe somebody else has an idea...

I have a source xml structure that comes from a legacy system. It is
somewhat similar to a database structure:

<data>
<fieldnames>
<name>Column 1</name>
<name>Column 2</name>
...
</fieldnames>
<rows>
<row id="1">
<value>Value 1</value>
<value>Value 2</value>
...
</row>
<row id="2">
....
</row>
</rows>
</data>

This should be tranformed to a structure much like this:

<data>
<record id="1">
<col1>Value 1</col1>
<col2>Value 2</col2>
...
</record>
<record id="2">
...
</record>
</data>

All of this wouldn't be too hard if I could hard-code the respective
column indices, i.e. if 'Column 1' was alwas at position 1 etc. But
unfortunately that is not always the case - the columns can 'switch
places' - and therefore I cannot select <valuetags according to their
'position()'.

I am at a loss here - any ideas?

Thanks,

Joerg

Aug 21 '06 #1
3 1138

jl wrote:
unfortunately that is not always the case - the columns can 'switch
places' - and therefore I cannot select <valuetags according to their
'position()'.
Select the <valueelement s according to their position(), but
determine this position at the beginning of run-time, by evaluating the
position() of the appropriate "Column 1" element and storing this in an
XSLT variable, for each column.

Aug 21 '06 #2
jl

Andy Dingley wrote:
jl wrote:
unfortunately that is not always the case - the columns can 'switch
places' - and therefore I cannot select <valuetags according to their
'position()'.

Select the <valueelement s according to their position(), but
determine this position at the beginning of run-time, by evaluating the
position() of the appropriate "Column 1" element and storing this in an
XSLT variable, for each column.
Hi Andy!

Thank you for the idea! Do you have a coding sample? Because I have no
clue how to do that (in XSL).

Regards,

Joerg

Aug 21 '06 #3

jl wrote:
Thank you for the idea! Do you have a coding sample?
Here's some scruffy code for the much easier way to do it, which takes
its ordering from the data order rather than the columns. As any
downstream processing _ought_ to use the XML element names rather than
implicit position, then it'll probably work for you.

Taking the order from the columns is a bit harder! (I might have time
to hack it later)
<xsl:template match="/" >

<xsl:variable name="dataset" select="documen t('')//d:data" />
<xsl:variable name="columns" select="$datase t/d:fieldnames/d:name" />

<data>
<xsl:for-each select="$datase t/d:rows/d:row" >
<record>
<xsl:attribut e name="id" ><xsl:value-of select="./@id"
/></xsl:attribute>

<xsl:for-each select="./d:value" >
<xsl:variable name="col-idx" select="positio n()" />
<xsl:variable name="column" select="$column s [position() =
$col-idx]" />
<xsl:variable name="column-name" select="transla te
(normalize-space ($column/text()), 'ABCDEFGHIJKLMN OPQRSTUVWXYZ ',
'abcdefghijklmn opqrstuvwxyz-')" />

<xsl:element name="{$column-name}" ><xsl:apply-templates
/></xsl:element>
</xsl:for-each>

</record>
</xsl:for-each>
</data>

</xsl:template>

Aug 23 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2713
by: Sergio del Amo | last post by:
Hi, I use the xslt functions provided by php. I am running in my computer the package xampp(www.apachefriends.org) which includes php/apache/mysql .. In this package the php includes the sablotron extension responsible for the xslt functions. The problem i have is that the obtained transformation is not the waited one. I try to proccess the same XML file with XSL file with a program called XMLspy and i obtained the desire and waited...
3
7929
by: pradeep gummi | last post by:
I have an XML FILE that is to be converted to Plain Text using an XSL file. Since I just want plain text, I do not want to set any root element during transformation.And if I do not any root element during transformation, it return s "java.lang.IllegalStateException: Root element not set" exception. If I add any element for the enclosed root, it works. Note: I am using XMLOutputter object of JDOM API, packages javax.xml.transform and...
4
2440
by: Kevin Dean | last post by:
I'm trying to create an XSL transformation that will strip out development-specific attributes from deployment descriptors and other XML files. I have already successfully done so with web.xml but I'm at a complete loss as to what is wrong with the one below. This is a very abbreviated server-config.wsdd: <?xml version="1.0" encoding="UTF-8"?> <deployment xmlns="http://xml.apache.org/axis/wsdd/"...
7
3248
by: CK | last post by:
Hello, I have the 60 MB XML string and I am coding a program in Visual Basic to run a XSL transformation on it. Currently, I'm using the Microsoft standard MSXML 2.0 to create a DOM document, load the XML, and then run the XSL transformation on it using the MSXML2.IXSLProcessor interface. The problem is that it kills my system because of an incredible amount of memory that it requires to do this (around 850 MB). Are there any other...
8
2370
by: Will | last post by:
I was thrust into XML about 2 weeks ago and don't know much yet. From another department in the corp I am receiving an XML file which concatenates nodes all on one line i.e. <car><make>ford</make><color>red</color><year>2001</year></car><car><make><mb> etc. etc. etc. Some lines are over 300 characters long. I need to translate this spagetti XML into something which is humanly readable. I probably need to use XSL however I'm not sure...
4
1886
by: Jyrki Keisala | last post by:
I have an XML file: <document> <record> <element1>Text</element1> <element2>More text</element2> <element3>Even more text</element3> <element4>Some text <link><title>Google</title><url> http://www.google.com</url></link> with a link.</element4>
2
1794
by: TomekR | last post by:
Hello ! I was developing xslt sheet lately and - experimenting - I made mistake resulting in that, the effect of the transformation is not well-formed xml document. I made these tests using XmlSpy and in output window I can see two parallel elements - according to "logic" of me sheet. The error of that document is that it doesn't have root element. Here is the contents of XmlSpy output window:
2
1822
by: HendrikLeder | last post by:
Hello everybody :-) Next year I`ll write my diploma in computer science for business (It`s a degree in Germany) and I`ve some questions about the topic. The diploma will handle about refactoring of xml-schemes and a transformation of all instances (the xml-documents) with XSLT-scripts. The idea of the diploma is, that in nearly every company xml schemes are used. And (like Heraklit said "Panta rhei" - everthing is flowing) the business...
1
1798
by: newbie | last post by:
This is probably a too general question, thanks for any feedback in advance. I am trying to write a class to do transformation on a data set. I want to make it easy to maintain in the long-run and would like to ask here for some advice. I want to have a class DataSet which holds the data to be transformed; I want to have a class Transformation which transform a DataSet object into another DataSet object. For the long-term,...
1
2208
by: boetke | last post by:
Hi all, I am attempting to create an xslt transformation which I am having trouble with. I am fairly new to xslt and am having a hard time getting my head around it. The original xml file looks like this: <DataSet ID="-5480"> <ObsGroup> <ClsItem name="AGE">2099</ClsItem> <ClsItem name="COMB_DATA">20</ClsItem> <ClsItem name="SEX">2</ClsItem> <ClsItem name="STATE">0</ClsItem>
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10595
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.