473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 1110

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 <valueelements 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 <valueelements 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="document('')//d:data" />
<xsl:variable name="columns" select="$dataset/d:fieldnames/d:name" />

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

<xsl:for-each select="./d:value" >
<xsl:variable name="col-idx" select="position()" />
<xsl:variable name="column" select="$columns [position() =
$col-idx]" />
<xsl:variable name="column-name" select="translate
(normalize-space ($column/text()), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ',
'abcdefghijklmnopqrstuvwxyz-')" />

<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
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...
3
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...
4
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...
7
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...
8
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....
4
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>...
2
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...
2
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...
1
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...
1
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.