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

XSLT - How to transform Element to Attribute?


I am newbie to XSL transformations. I am finding it hard to work out the XSL transformation for the following XML. Can someone suggest me how to approach this XML or in general how should I start up if given a XML?


Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <Batch BatchCreated="" TransType="TA" xmlns="http://www.w3.org/2001/xmlns">
  3. <TA>
  4. <Id>111</Id>
  5. <recipient>ABC Corp.</recipient>
  6. </TA>
  7. <CL>
  8. <id>12123</id>
  9. <claimNumber>1023-1</claimNumber>
  10. <code>Other</code>
  11. <dateReceived>01/24/2007 14:18:50</dateReceived>
  12. <policyLimit>352,000</policyLimit>
  13. <covName>Building</covName>
  14. </CL>
  15. <Contacts>
  16. <Insured>
  17. <type>client</type>
  18. <InsName>JOHN</InsName>
  19. <street>12 WALKER ST</street>
  20. <city>SIGMUND PARK</city>
  21. <state>KS</state>
  22. <country>US</country>
  23. <postal>67208</postal>
  24. <InsHomeNumber>1212151423</InsHomeNumber>
  25. <InsWorkNumber>55221155213</InsWorkNumber>
  26. <WorkExtn>123</WorkExtn>
  27. </Insured>
  28. <Claimant>
  29. <type>claimant</type>
  30. <name>MARK</name>
  31. </Claimant>
  32. </Contacts>
  33. </Batch>

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <INFO>
  3. <TA recipient="ABC Corp." Id="111" />
  5. <CONTACT type="Client" name="JOHN">
  7. <ADDRESS type="Property" city="SIGMUND PARK" street=">12 WALKER ST" state="KS" postal="67208" country="US" />
  10. <PHONE type="Home" number="1212151423" extension="" />
  11. <PHONE type="Business" number="55221155213" extension="123" />
  13. </CONTACT>
  14. <CONTACT type="Claimant" name="MARK" />
  15. </CONTACTS>
  16. <LOSSDETAILS dateReceived="2007-01-24T14:18:50">
  17. <LOSS claimNumber="1023-1">
  19. <COVERAGE id="12123" covName="Building" policyLimit="3520000" />
  20. </COVERAGES>
  21. <TOL desc="Other" code="Other"></TOL>
  22. </LOSS>
  24. </INFO>
Dec 26 '08 #1
9 8347
8,658 Expert Mod 8TB
basicly you need to figure out, which data structure from the input belong to which data structure in the output. use xslt to a) get the data from the former and b) write the data to the latter.

best explained in an example. let's take the <ta> tag from the input
Expand|Select|Wrap|Line Numbers
  1. <TA>
  2. <Id>111</Id>
  3. <recipient>ABC Corp.</recipient>
  4. </TA>
this will be the following in the output:
Expand|Select|Wrap|Line Numbers
  1. <TA recipient="ABC Corp." Id="111" />
so the <recipient> tag goes to the recipient attribute (same for id)
to put this into XSL
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="w3c:TA">
  2.   <TA recipient="{w3c:recipient/text()}" Id="{w3c:Id/text()}"/>
  3. </xsl:template>
what does that mean:
  • w3c:TA
    the element <TA> with its namespace (the one you defined in the <Batch> element (you can choose any namespace prefix you like).
    the template will be called if the TA node is processed
  • recipient="{w3c:recipient/text()}"
    write an attribute named "recipient" and write its content as computed from the text node of the <recipient> child node (again with namespace)
this way you can write each target element.

start the whole whole process
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="/">
  2.   <xsl:apply-templates/>
  3. </xsl:template>
don't forget to define your namespace:
Expand|Select|Wrap|Line Numbers
  1. <xsl:stylesheet version="1.0"
  2. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:w3c="http://www.w3.org/2001/xmlns">
Dec 26 '08 #2
Hi Dormilich,

That was great !

Hope I can start up with this basic idea.

Will post bak if I get stuck up.Thnx :-) .

Dec 26 '08 #3

Referring to my previous post - how can i apply a separate template (styling) for an child element inside <xsl:template>?

For eg., if I need to apply a separate template for the contact of type 'Claimant' how can I do that?

This was what I trying but it changes the hierarchy of the XML:

Expand|Select|Wrap|Line Numbers
  1.   <xsl:template match ="Insured">
  2.     <CONTACTS>
  3.       <CONTACT type="{type/text()}" name="{name/text()}">
  4.         <ADDRESSES>
  5.           <ADDRESS type="property" city="{city/text()}" street="{street/text()}" state="{state/text()}" postal="{postal/text()}" country="US">            
  6.           </ADDRESS>
  7.           <CONTACTMETHODS>
  8.             <PHONE type="Home" number="{HomeNumber/text()}"></PHONE>
  9.             <PHONE type="Business" number="{WorkNumber/text()}" extension="{WorkExt/text()}"></PHONE>
  10.           </CONTACTMETHODS>
  11.         </ADDRESSES>
  12.       </CONTACT>
  13.       <xsl:apply-templates select ="Claimant"></xsl:apply-templates>
  14.     </CONTACTS>    
  15.   </xsl:template>
  17.   <xsl:template match ="Claimant">
  18.     <CONTACT type="{type/text()}" name="{name/text()}">
  19.       </CONTACT>
  20.   </xsl:template>
This is causing the second <CONTACT> element to be a child of the root element rather than being the child of <CONTACTS>.
Dec 31 '08 #4
8,658 Expert Mod 8TB
you could try a named template.

Dec 31 '08 #5
Can you quote some example?

Dec 31 '08 #6
8,658 Expert Mod 8TB
Named Template Example

Dec 31 '08 #7

Dec 31 '08 #8

Is there any other way to do this? I had been trying to achieve the above XML for a while and nothing helped me out.

Any suggestions on how to implement it?
Jan 6 '09 #9
2,057 Expert 2GB
My guess is you'd need apply-templates to get the elements in the same order. Wasn't going to post this, since it didn't totally apply but anyways:

generic element -> attribute template
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>
  3. <xsl:template match="*">
  4.   <xsl:copy>
  5.     <xsl:for-each select="*[not(*)]">
  6.       <xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
  7.     </xsl:for-each>
  8.     <xsl:apply-templates select="*[*]"/>
  9.   </xsl:copy>
  10. </xsl:template>
  11. </xsl:stylesheet>
Specific to your problem:
1. Since you're getting rid of the namespace, replace
<xsl:copy> with <xsl:element name="local-name()">

2. Remove unnecessary nodes: eg CL, etc... seperated by |
<xsl:template match="CL|OtherNode|etc"/>

3. Have custom templates accordingly:
Expand|Select|Wrap|Line Numbers
  1. xmlns:w3="http://www.w3.org/2001/xmlns"
  2. ...
  3. <xsl:template match="w3:Batch"> 
  4.   <INFO>
  5.     <xsl:apply-templates/>
  6.   </INFO>
  7. </xsl:template>
  8. <xsl:template match ="w3:Claimant"> 
  9.   <CONTACT type="{type/text()}" name="{name/text()}"/> 
  10. </xsl:template> 
  11. <xsl:template match="w3:Insured"><!-- remove this node, but continue on. -->
  12.   <xsl:apply-templates/>
  13. </xsl:template>
Don't know where you're getting loss details from. Assuming it's somewhere else in unposted source code.
Jan 6 '09 #10

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

Similar topics

by: nanookfan | last post by:
Hi all, I'm having a bizarre problem converting XML files to HTML using an XSLT. The problem is only occuring in my Netscape 7.0 browser. What makes it more bizarre is that it is only...
by: rush | last post by:
I have a DTD that defines new elements "mytextfield" and "mysn", and does it as an extension to XHTML. The idea is that my XML markup is actually valid XHTML according to my DTD. This all works...
by: Wayne Lian via .NET 247 | last post by:
Hi all, Just wonder anyone have encountered this problem before?I tried using XMLSPY debugger and I can get the correct outputfor my XSLT transformation, however in .net, aftertransformation the...
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID...
by: One Handed Man \( OHM - Terry Burns \) | last post by:
I've been battling with this stupid problem for hours now. WebApp: Trying to do a simple transformation using XSLT to a Web Page, but it just failes without an error message ( In other words,...
by: David S. Alexander | last post by:
How can I do simple subtraction in an XSLT. I want to read a few attribute values from an XML document, calculate their difference, and transform that value to an attribute in the XML output...
by: arnold | last post by:
Hi, I've been knocking my head against the wall trying to create an XSL transform to perform "normalizations" of a set of XML files that have a common structure. % XML file before transform
by: n.phelge | last post by:
I need to perform an XSLT to set the namespace on some XML and I need to preserve the original document line formatting to assist with error handling (so the line number from any schema validation...
by: astroboiii | last post by:
New to the whole xml thing and finding w3schools to be an excellent resource. Now down to my question: I have several xml files I need to parse through and grab relevant information from and...
by: John Larson | last post by:
Hi All, I am some information from INSPEC database records in XML to build a relational database of my own. I am currently trying to extract information by doing an XSLT transform of the XML...
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...
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.