423,323 Members | 1,789 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,323 IT Pros & Developers. It's quick & easy.

How to transform XML attributes values into XML elements?

P: 1
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
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 921
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

Post your reply

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