473,395 Members | 1,742 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,395 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 5206
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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,...

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.