473,326 Members | 2,081 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.

How to transform XML attributes values into XML elements?

Platform: Informatica 9.6.1 (XML parcer).

I have a generic XML source file where column names (element names) are stored as attribute (AttributeID) values. I need to write XSLT file and Java code to convert attributes (AttributeID) values into the element (Column_1, Column_2, etc.) and elements values so it can be correctly read by Informatica XML parser. I am new to XSLT.

Here is the sample of the XML source file:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Products ExportTime="2017-08-21 11:24:43" ExportContext="Context1" ContextID="Context1" WorkspaceID="Main" UseContextLocale="false">
  3.     <Product Product_ID="1000">
  4.         <Entities>
  5.             <Entity Entity_ID="100" UserTypeID="SourceRecord1" ParentID="Source Records10" Selected="false1" Referenced="true1">
  6.             <Name>Name_1</Name>
  7.                 <Values>
  8.                     <Value AttributeID="Column_1">Value_1</Value>
  9.                     <Value AttributeID="Column_2">Value_2</Value>
  10.                     <Value AttributeID="Column_3" Column_4="Value_4">Value_3</Value>
  11.                 </Values>
  12.             </Entity>
  13.             <Entity Entity_ID="101" UserTypeID="SourceRecord2" ParentID="Source Records20" Selected="false2" Referenced="true2">
  14.             <Name>Name_2</Name>
  15.                 <Values>
  16.                     <Value AttributeID="Column_1">Value_5</Value>
  17.                     <Value AttributeID="Column_2">Value_6</Value>
  18.                     <Value AttributeID="Column_3" Column_4="Value_8">Value_7</Value>
  19.                 </Values>
  20.             </Entity>
  21.         </Entities>
  22.     </Product>
  23.     <Product Product_ID="1001">
  24.         <Entities>
  25.             <Entity Entity_ID="102" UserTypeID="SourceRecord3" ParentID="Source Records30" Selected="false3" Referenced="true3">
  26.             <Name>Name_3</Name>
  27.                 <Values>
  28.                     <Value AttributeID="Column_1">Value_9</Value>
  29.                     <Value AttributeID="Column_2">Value_10</Value>
  30.                     <Value AttributeID="Column_3" Column_4="Value_12">Value_11</Value>
  31.                 </Values>
  32.             </Entity>
  33.             <Entity Entity_ID="103" UserTypeID="SourceRecord4" ParentID="Source Records40" Selected="false4" Referenced="true4">
  34.             <Name>Name_4</Name>
  35.                 <Values>
  36.                     <Value AttributeID="Column_1">Value_13</Value>
  37.                     <Value AttributeID="Column_2">Value_14</Value>
  38.                     <Value AttributeID="Column_3" Column_4="Value_16">Value_15</Value>
  39.                 </Values>
  40.             </Entity>
  41.         </Entities>
  42.     </Product>
  43. </Products>
I have to transform this file into the normally formed one (with elements and their value primarily):

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Products ExportTime="2017-08-21 11:24:43" ExportContext="Context1" ContextID="Context1" WorkspaceID="Main" UseContextLocale="false">
  3.     <Product Product_ID="1000">
  4.         <Entities>
  5.             <Entity Entity_ID="100" UserTypeID="SourceRecord" ParentID="Source Records" Selected="false" Referenced="true">
  6.                 <Name>Name_1</Name>
  7.                 <Item ItemID="1238240">
  8.                     <Column_1>Value_1</Column_1>
  9.                     <Column_2>Value_2</Column_2>
  10.                     <Column_3>Value_3</Column_3>
  11.                     <Column_4>Value_4</Column_4>
  12.                 </Item>
  13.             </Entity>
  14.             <Entity Entity_ID="101" UserTypeID="SourceRecord" ParentID="Source Records" Selected="false" Referenced="true">
  15.                 <Name>Name_2</Name>
  16.                 <Item>
  17.                     <Column_1>Value_5</Column_1>
  18.                     <Column_2>Value_6</Column_2>
  19.                     <Column_3>Value_7</Column_3>
  20.                     <Column_4>Value_8</Column_4>
  21.                 </Item>
  22.             </Entity>
  23.         </Entities>
  24.     </Product>
  25.     <Product Product_ID="1001">
  26.         <Entities>
  27.             <Entity Entity_ID="102" UserTypeID="SourceRecord" ParentID="Source Records" Selected="false" Referenced="true">
  28.                 <Name>Name_3</Name>
  29.                 <Item ItemID="1238240">
  30.                     <Column_1>Value_9</Column_1>
  31.                     <Column_2>Value_10</Column_2>
  32.                     <Column_3>Value_11</Column_3>
  33.                     <Column_4>Value_12</Column_4>
  34.                 </Item>
  35.             </Entity>
  36.             <Entity Entity_ID="103" UserTypeID="SourceRecord" ParentID="Source Records" Selected="false" Referenced="true">
  37.                 <Name>Name_4</Name>
  38.                 <Item>
  39.                     <Column_1>Value_13</Column_1>
  40.                     <Column_2>Value_14</Column_2>
  41.                     <Column_3>Value_15</Column_3>
  42.                     <Column_4>Value_16</Column_4>
  43.                 </Item>
  44.             </Entity>
  45.         </Entities>
  46.     </Product>
  47. </Products>
Thank you for any help with XSLT code or hits in advance!
Sep 21 '17 #1
1 5161
Luuk
1,047 Expert 1GB
Xmlstarlet is a tool which can create a XLST-stylesheets, see example below.
It can help in getting to know more about XSLT in a do-it-yourself manner ;)

From the website (http://xmlstar.sourceforge.net) :
[QUOOT]XMLStarlet is a set of command line utilities (tools) which can be used to transform, query, validate, and edit XML documents and files using simple set of shell commands[/QUOOT]


Expand|Select|Wrap|Line Numbers
  1. C:\temp>xml sel -t -v //Name  source.xml
  2. Name_1
  3. Name_2
  4. Name_3
  5. Name_4
  6. C:\temp>xml sel -C -t -v //Name  source.xml
  7. <?xml version="1.0"?>
  8. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common" version="1.0" extension-element-prefixes="exslt">
  9.   <xsl:output omit-xml-declaration="yes" indent="no"/>
  10.   <xsl:template match="/">
  11.     <xsl:call-template name="value-of-template">
  12.       <xsl:with-param name="select" select="//Name"/>
  13.     </xsl:call-template>
  14.   </xsl:template>
  15.   <xsl:template name="value-of-template">
  16.     <xsl:param name="select"/>
  17.     <xsl:value-of select="$select"/>
  18.     <xsl:for-each select="exslt:node-set($select)[position()&gt;1]">
  19.       <xsl:value-of select="'
  20. '"/>
  21.       <xsl:value-of select="."/>
  22.     </xsl:for-each>
  23.   </xsl:template>
  24. </xsl:stylesheet>
  25.  
Oct 29 '17 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Gooseman | last post by:
Hi Just getting into XSD and have some questions: If I have several different elements that all have identical attibutes, what is the best way of conveying that in the XSD? Is there a way...
11
by: Jeff Kish | last post by:
Just wondering if it was possible to xquery an xml file to retrieve all elements that had a given text value or all elements where any attribute had a value of interest. I guess I could figure...
2
by: Paul Dunstone | last post by:
Hi group I am trying to add a custom attribute to all .NET elements such as textboxes, labels, datagrids etc. I want to add an attribute called 'key'. I already have the code working for the...
3
by: Stephan Brunner | last post by:
Hi I have created two flavors of an XSLT stylesheet to transform all attributes of an XML document to elements: They both work as expected with MSXML and XMLSPY but throw an exception ...
2
by: Novice | last post by:
Hi all, I have to decide on an XML structure going forward. The structure is going to house a large amount of data. In the past I've always just used the philosophy of "when in doubt use...
3
by: asjad | last post by:
Is there any way through which attributes of elements along with their default value and possible values can be retrieved from the schema using C#. Any help will really be appreciated.
6
by: Jakub.Bednarczuk | last post by:
Hallo everybody I have the problem with getting attributes values and also attributes names. I am reading an xml file with DOM. Lets see an example: file I read <root> <Def></Def>...
0
by: David Lozzi | last post by:
Howdy, I just had a small explosion in my brain, and now i'm a little more confused than I was before. I'm fairly new to XML, but understand it for the most part in regards to formatting. I have...
3
by: messagewalker | last post by:
Hello All, I am stuck with a problem. I have an xml file as follows <parent> <publisher> <name>somename</name> <book1>data1</book1> <book2>data2</book2> <book3>data3</book3>
2
by: afshar | last post by:
I want to use XML to import/export some data. My data is like a database record like an employee information. I'm wondering which is better to use: saving data in attributes or saving them in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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.