473,396 Members | 1,714 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,396 software developers and data experts.

Transforming Excel "XML"

Hello!

I have an Excel XML feed from an Enterprise PDM system that is
basically a flat text file with XML tags:

<?xml version="1.0" encoding="UTF-8"?>
<Import>
<Row>
<ID>1</ID>
<Level>0</Level>
<PartNumber>2211845-500</PartNumber>
<ItemNum>1</ItemNum>
</Row>
<Row>
<ID>2</ID>
<Level>1</Level>
<PartNumber>2211845</PartNumber>
<ItemNum>0</ItemNum>
<Qty>0</Qty>
<Rev>1</Rev>
<SubsOK>Yes</SubsOK>
<ItemLifecyclePhase>Engineering Released</ItemLifecyclePhase>
<ItemType>[Drawing</ItemType>
</Row>
<Row>
<ID>3</ID>
<Level>1</Level>
<PartNumber>2213090</PartNumber>
<ItemNum>0</ItemNum>
<Qty>0</Qty>
<Rev>A</Rev>
<SubsOK>Yes</SubsOK>
<ItemLifecyclePhase>Production Released</ItemLifecyclePhase>
<ItemType>[Specification</ItemType>
</Row>
<Row>
<ID>4</ID>
<Level>2</Level>
<PartNumber>MIL-C-5541</PartNumber>
<ItemNum>0</ItemNum>
<Qty>0</Qty>
<Rev>E</Rev>
<SubsOK>Yes</SubsOK>
<ItemLifecyclePhase>Production Released</ItemLifecyclePhase>
<ItemType>[Standard</ItemType>
</Row>
<Row>
<ID>5</ID>
<Level>1</Level>
<PartNumber>2211845-001</PartNumber>
<ItemNum>1</ItemNum>
<Qty>0.001</Qty>
<UM>EA</UM>
<Rev>1</Rev>
<MakeBuy>M</MakeBuy>
<SubsOK>Yes</SubsOK>
<ItemLifecyclePhase>Engineering Released</ItemLifecyclePhase>
<ItemType>PART</ItemType>
</Row>
<Row>
<ID>6</ID>
<Level>2</Level>
<PartNumber>2211845</PartNumber>
<ItemNum>0</ItemNum>
<Qty>0</Qty>
<Rev>1</Rev>
<SubsOK>Yes</SubsOK>
<ItemLifecyclePhase>Engineering Released</ItemLifecyclePhase>
<ItemType>[Drawing</ItemType>
</Row>
<Row>
<ID>7</ID>
<Level>2</Level>
<PartNumber>MIL-C-7438</PartNumber>
<ItemNum>0</ItemNum>
<Qty>0</Qty>
<Rev>G</Rev>
<SubsOK>Yes</SubsOK>
<ItemLifecyclePhase>Production Released</ItemLifecyclePhase>
<ItemType>[Standard</ItemType>
</Row>
<Row>
<ID>8</ID>
<Level>3</Level>
<PartNumber>MIL-C-7439</PartNumber>
<ItemNum>0</ItemNum>
<Qty>0</Qty>
<Rev>G</Rev>
<SubsOK>Yes</SubsOK>
<ItemLifecyclePhase>Production Released</ItemLifecyclePhase>
<ItemType>[Standard</ItemType>
</Row>
</Import>

The items are read in order and related through the LEVEL tag.
Therefore the desired structure of a transformed XML file would be:

Level0 - Root
Level1
Level1
|--------|
| Level2
|
Level1
|--------|------------|
Level2 Level2
|--------|
Level3

I have tried to access the "subLevels" in the following manner,
but haven't had success:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>

<xsl:template match="/">
<Root>
<xsl:apply-templates/>
</Root>
</xsl:template>
<xsl:template match="Import">
<Assembly>
<AssemblyName>
<xsl:value-of select="Row/ReferenceNo"/>
</AssemblyName>
</Assembly>
<xsl:apply-templates select="Row"/>
</xsl:template>
<xsl:template match="Row">
<xsl:if test="Level = 1">
<xsl:for-each select=".">
<PartNum>
<xsl:value-of select="PartNumber"/>
<PartType>
<xsl:value-of select="ItemType"/>
<LifeCyclePhase>
<xsl:value-of select="ItemLifecyclePhase"/>
</LifeCyclePhase>
</PartType>
<xsl:if test="following-sibling::Level = 2">
<SubPart>
<PartNum>
<xsl:value-of select="PartNumber"/>
<PartType>
<xsl:value-of select="ItemType"/>
<LifeCyclePhase>
<xsl:value-of select="ItemLifecyclePhase"/>
</LifeCyclePhase>
</PartType>
<xsl:if test="following-sibling::Level = 3">
<SubPart2>
<PartNum>
<xsl:value-of select="PartNumber"/>
<PartType>
<xsl:value-of select="ItemType"/>
<LifeCyclePhase>
<xsl:value-of select="ItemLifecyclePhase"/>
</LifeCyclePhase>
</PartType>
</PartNum>
</SubPart2>
</xsl:if>
</PartNum>
</SubPart>
</xsl:if>
</PartNum>
</xsl:for-each>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

I can't seem to get the "if test following-sibling" syntax to work
correctly. I've looked all over the web for examples but no dice.

Does anyone have any pointers they could offer, or maybe a different
approach that might work for this example?

Thanks in advance for any help provided!

Jul 20 '05 #1
2 1924
Tempore 05:58:50, die Thursday 10 February 2005 AD, hinc in foro {comp.text.xml} scripsit Ferd Biffle <ep***@attbi.com>:
The items are read in order and related through the LEVEL tag.
Therefore the desired structure of a transformed XML file would be:

Level0 - Root
Level1
Level1
|--------|
| Level2
|
Level1
|--------|------------|
Level2 Level2
|--------|
Level3

I have tried to access the "subLevels" in the following manner,
but haven't had success:

<code/> I would use a nested decision structure packed up in one template like this, especially if the nesting level of your input XML may increase.

Maybe you could try a recursive solution like this:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:key name="row" match="Row" use="generate-id(preceding-sibling::Row[current()/Level - Level=1][1])"/>

<xsl:template match="/">
<Root>
<xsl:apply-templates/>
</Root>
</xsl:template>

<xsl:template match="Import">
<Assembly>
<AssemblyName>
<xsl:value-of select="Row/ReferenceNo"/>
</AssemblyName>
</Assembly>
<xsl:apply-templates select="Row[number(Level)=0]"/>
</xsl:template>

<xsl:template match="Row">
<xsl:element name="SubPart{Level}">
<PartNum>
<xsl:value-of select="PartNumber"/>
<PartType>
<xsl:value-of select="ItemType"/>
<LifeCyclePhase>
<xsl:value-of select="ItemLifecyclePhase"/>
</LifeCyclePhase>
</PartType>
<xsl:apply-templates select="key('row',generate-id())"/>
</PartNum>
</xsl:element>
</xsl:template>

</xsl:stylesheet>
output:
<Root>
<Assembly>
<AssemblyName></AssemblyName>
</Assembly>
<SubPart0>
<PartNum>2211845-500<PartType><LifeCyclePhase></LifeCyclePhase></PartType><SubPart1>
<PartNum>2211845<PartType>[Drawing<LifeCyclePhase>Engineering Released</LifeCyclePhase></PartType></PartNum>
</SubPart1><SubPart1>
<PartNum>2213090<PartType>[Specification<LifeCyclePhase>Production Released</LifeCyclePhase></PartType><SubPart2>
<PartNum>MIL-C-5541<PartType>[Standard<LifeCyclePhase>Production Released</LifeCyclePhase></PartType></PartNum>
</SubPart2></PartNum>
</SubPart1><SubPart1>
<PartNum>2211845-001<PartType>PART<LifeCyclePhase>Engineering Released</LifeCyclePhase></PartType><SubPart2>
<PartNum>2211845<PartType>[Drawing<LifeCyclePhase>Engineering Released</LifeCyclePhase></PartType></PartNum>
</SubPart2><SubPart2>
<PartNum>MIL-C-7438<PartType>[Standard<LifeCyclePhase>Production Released</LifeCyclePhase></PartType><SubPart3>
<PartNum>MIL-C-7439<PartType>[Standard<LifeCyclePhase>Production Released</LifeCyclePhase></PartType></PartNum>
</SubPart3></PartNum>
</SubPart2></PartNum>
</SubPart1></PartNum>
</SubPart0>
</Root>

I can't seem to get the "if test following-sibling" syntax to work
correctly.

The context node is a 'Row' element, when you prefer this test, so you'll never get a following sibling named 'Level'.
Probably you meant:
<xsl:if test="Level = '2'" />
or
<xsl:if test="number(Level) = 2" />
regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Vincit omnia simplicitas
Keep it simple
Jul 20 '05 #2
Hey Joris!

That did the trick - I banged my head on this one all yesterday
afternoon. I'm jumping back into XML\XSL
after taking an MOC class a year and a half ago. Amazing what you
forget :-)

Anyhow - thanks for the lesson!

Ferd
Joris Gillis wrote:
Tempore 05:58:50, die Thursday 10 February 2005 AD, hinc in foro {comp.text.xml} scripsit Ferd Biffle <ep***@attbi.com>:
The items are read in order and related through the LEVEL tag.
Therefore the desired structure of a transformed XML file would be:

Level0 - Root
Level1
Level1
|--------|
| Level2
|
Level1
|--------|------------|
Level2 Level2
|--------|
Level3

I have tried to access the "subLevels" in the following manner,
but haven't had success:

<code/> I would use a nested decision structure packed up in one template

like this, especially if the nesting level of your input XML may
increase.
Maybe you could try a recursive solution like this:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:key name="row" match="Row" use="generate-id(preceding-sibling::Row[current()/Level -
Level=1][1])"/>
<xsl:template match="/">
<Root>
<xsl:apply-templates/>
</Root>
</xsl:template>

<xsl:template match="Import">
<Assembly>
<AssemblyName>
<xsl:value-of select="Row/ReferenceNo"/>
</AssemblyName>
</Assembly>
<xsl:apply-templates select="Row[number(Level)=0]"/>
</xsl:template>

<xsl:template match="Row">
<xsl:element name="SubPart{Level}">
<PartNum>
<xsl:value-of select="PartNumber"/>
<PartType>
<xsl:value-of select="ItemType"/>
<LifeCyclePhase>
<xsl:value-of select="ItemLifecyclePhase"/>
</LifeCyclePhase>
</PartType>
<xsl:apply-templates select="key('row',generate-id())"/>
</PartNum>
</xsl:element>
</xsl:template>

</xsl:stylesheet>
output:
<Root>
<Assembly>
<AssemblyName></AssemblyName>
</Assembly>
<SubPart0>
<PartNum>2211845-500<PartType><LifeCyclePhase></LifeCyclePhase></PartType><SubPart1> <PartNum>2211845<PartType>[Drawing<LifeCyclePhase>Engineering Released</LifeCyclePhase></PartType></PartNum> </SubPart1><SubPart1>
<PartNum>2213090<PartType>[Specification<LifeCyclePhase>Production
Released</LifeCyclePhase></PartType><SubPart2> <PartNum>MIL-C-5541<PartType>[Standard<LifeCyclePhase>Production
Released</LifeCyclePhase></PartType></PartNum> </SubPart2></PartNum>
</SubPart1><SubPart1>
<PartNum>2211845-001<PartType>PART<LifeCyclePhase>Engineering Released</LifeCyclePhase></PartType><SubPart2> <PartNum>2211845<PartType>[Drawing<LifeCyclePhase>Engineering Released</LifeCyclePhase></PartType></PartNum> </SubPart2><SubPart2>
<PartNum>MIL-C-7438<PartType>[Standard<LifeCyclePhase>Production
Released</LifeCyclePhase></PartType><SubPart3> <PartNum>MIL-C-7439<PartType>[Standard<LifeCyclePhase>Production
Released</LifeCyclePhase></PartType></PartNum> </SubPart3></PartNum>
</SubPart2></PartNum>
</SubPart1></PartNum>
</SubPart0>
</Root>

I can't seem to get the "if test following-sibling" syntax to work
correctly. The context node is a 'Row' element, when you prefer this test, so

you'll never get a following sibling named 'Level'. Probably you meant:
<xsl:if test="Level = '2'" />
or
<xsl:if test="number(Level) = 2" />
regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041) Vincit omnia simplicitas
Keep it simple


Jul 20 '05 #3

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

Similar topics

0
by: David Furey | last post by:
Hi, I am using the XSLT document to filter records from an XML document The XSL is shown below: I have a parameter named "search-for" that is used to bring back a list of Vendor Names that...
4
by: David List | last post by:
I'm working on a analyzation/design project that is supposed to uncover the intricacies and implications in entering the market for systems used by public administrations here in Denmark. XML...
3
by: Tjerk Wolterink | last post by:
Hello, I've xml code that looks like this: <?xml version="1.0" encoding="ISO-8859-1"?> <xc:xcontent xmlns:xc="http://www.wolterinkwebdesign.com/xml/xcontent"...
5
by: Arturius mac Aidan | last post by:
I'm not sure if this has been addressed in more recent developments, but I'm reading the MathML specification which states "For example, it is not possible in XML to specify that the first child be...
3
by: Amy L. | last post by:
I am using the xml serializer to serialize the objects out of a collection into a file. The object that is being serialized is a class in my application. When I serialize my class it produces the...
0
by: sjallard | last post by:
Dear all, This is my first work with the XML extenders: I'll have to decompose an XML file into a DB2 table, and "flatten" it in the same time, like in the example at the end of this message. I...
0
by: jeoffh | last post by:
Background: I am trying to "merge" some attributes into an existing XML column in my MS SQL 2005 database. The general idea is that I have an XML column in a table and I would like to update/delete...
0
by: wildman | last post by:
Trying to read XML files from SSIS and load into SQL Server. I tested this before and it was working before I placed in a forevery contrainer. also, my simple xml file had to be retyped cause I...
2
by: CGatto | last post by:
Hi, We have just started getting the following error during compiles of our forms-based application. We are developing in VS2008, VB.Net, with Team Foundation Server-based source control. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.