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

XSLT - How to transform Element to Attribute?

SPanicker
Hi,

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?

SOURCE 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>
  34.  
TRANSFORMED FINAL XML (what i need):

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <INFO>
  3. <TA recipient="ABC Corp." Id="111" />
  4. <CONTACTS>
  5. <CONTACT type="Client" name="JOHN">
  6. <ADDRESSES>
  7. <ADDRESS type="Property" city="SIGMUND PARK" street=">12 WALKER ST" state="KS" postal="67208" country="US" />
  8. </ADDRESSES>
  9. <CONTACTMETHODS>
  10. <PHONE type="Home" number="1212151423" extension="" />
  11. <PHONE type="Business" number="55221155213" extension="123" />
  12. </CONTACTMETHODS>
  13. </CONTACT>
  14. <CONTACT type="Claimant" name="MARK" />
  15. </CONTACTS>
  16. <LOSSDETAILS dateReceived="2007-01-24T14:18:50">
  17. <LOSS claimNumber="1023-1">
  18. <COVERAGES>
  19. <COVERAGE id="12123" covName="Building" policyLimit="3520000" />
  20. </COVERAGES>
  21. <TOL desc="Other" code="Other"></TOL>
  22. </LOSS>
  23. </LOSSDETAILS>
  24. </INFO>
  25.  
Regards,
SP.
Dec 26 '08 #1
9 8346
Dormilich
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">
regards
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 :-) .


Regards,
SP.
Dec 26 '08 #3
Hi,

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>
  16.  
  17.   <xsl:template match ="Claimant">
  18.     <CONTACT type="{type/text()}" name="{name/text()}">
  19.       </CONTACT>
  20.   </xsl:template>
  21.  
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
Dormilich
8,658 Expert Mod 8TB
you could try a named template.

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

Regards,
SP.
Dec 31 '08 #6
Dormilich
8,658 Expert Mod 8TB
sure
Named Template Example

regards
Dec 31 '08 #7
Thnx.

Regards,
SP.
Dec 31 '08 #8
Hi,

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
jkmyoung
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>
  12.  
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>
  14.  
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

2
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...
3
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...
1
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...
7
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...
7
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,...
4
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...
1
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
4
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...
2
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...
6
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...
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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
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 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.