XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
amith cnSubject: XSL mapping for XMLs having different XML element names in input and output XMLs
Author: amith cn
Date: 10 Jul 2007 02:57 PM
Hi,


I have mapped xmls( here xml elements in 2 xmls differ in their name) using an XSL

first.xml
<ReservationControlInformationSegment>
<CompanyId>AA</CompanyId>
<ReservationControlNumber>ABC</ReservationControlNumber>
<CompanyId>BB</CompanyId>
<ReservationControlNumber>DEF</ReservationControlNumber>
<CompanyId>CC</CompanyId>
<ReservationControlNumber>GHI</ReservationControlNumber>
</ReservationControlInformationSegment>

second.xml
<RCI>
<RCI01-ReservationControlInformation>
<RCI0101-CompanyIdentification>AA</RCI0101-CompanyIdentification>
<RCI0102-ReservationControlNumber>ABC</RCI0102-ReservationControlNumber>
</RCI01-ReservationControlInformation>

<RCI02-ReservationControlInformation>
<RCI0201-CompanyIdentification>BB</RCI0201-CompanyIdentification>
<RCI0202-ReservationControlNumber>DEF</RCI0202-ReservationControlNumber>
</RCI02-ReservationControlInformation>

<RCI03-ReservationControlInformation>
<RCI0301-CompanyIdentification>CC</RCI0301-CompanyIdentification>
<RCI0302-ReservationControlNumber>GHI</RCI0302-ReservationControlNumber>
</RCI03-ReservationControlInformation>

</RCI>

Below are the XSLs that we have developed. We tried to use for:each in the XSL but we were not able to map it.
Is there a better way to map using for:each or any other sytnax available.

Here if XMLs have huge repeating data(we have only showed 3 repeatitions) then XSL will become huge. We want to reduce the XSL

XSL that maps from first.xml to second.xml
<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<RCI>
<RCI01-ReservationControlInformation>
<RCI0101-CompanyIdentification>
<xsl:value-of select="ReservationControlInformationSegment/CompanyId[1]"/>
</RCI0101-CompanyIdentification>
<RCI0102-ReservationControlNumber>
<xsl:value-of select="ReservationControlInformationSegment/ReservationControlNumber[1]"/>
</RCI0102-ReservationControlNumber>
</RCI01-ReservationControlInformation>
<RCI02-ReservationControlInformation>
<RCI0201-CompanyIdentification>
<xsl:value-of select="ReservationControlInformationSegment/CompanyId[2]"/>
</RCI0201-CompanyIdentification>
<RCI0202-ReservationControlNumber>
<xsl:value-of select="ReservationControlInformationSegment/ReservationControlNumber[2]"/>
</RCI0202-ReservationControlNumber>
</RCI02-ReservationControlInformation>
<RCI03-ReservationControlInformation>
<RCI0301-CompanyIdentification>
<xsl:value-of select="ReservationControlInformationSegment/CompanyId[3]"/>
</RCI0301-CompanyIdentification>
<RCI0302-ReservationControlNumber>
<xsl:value-of select="ReservationControlInformationSegment/ReservationControlNumber[3]"/>
</RCI0302-ReservationControlNumber>
</RCI03-ReservationControlInformation>
</RCI>
</xsl:template>
</xsl:stylesheet>

XSL that maps second.xml to first.xml
<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<ReservationControlInformationSegment>
<CompanyId>
<xsl:value-of select="RCI/RCI01-ReservationControlInformation/RCI0101-CompanyIdentification"/>
</CompanyId>
<ReservationControlNumber>
<xsl:value-of select="RCI/RCI01-ReservationControlInformation/RCI0102-ReservationControlNumber"/>
</ReservationControlNumber>
<CompanyId>
<xsl:value-of select="RCI/RCI02-ReservationControlInformation/RCI0201-CompanyIdentification"/>
</CompanyId>
<ReservationControlNumber>
<xsl:value-of select="RCI/RCI02-ReservationControlInformation/RCI0202-ReservationControlNumber"/>
</ReservationControlNumber>
<CompanyId>
<xsl:value-of select="RCI/RCI03-ReservationControlInformation/RCI0301-CompanyIdentification"/>
</CompanyId>
<ReservationControlNumber>
<xsl:value-of select="RCI/RCI03-ReservationControlInformation/RCI0302-ReservationControlNumber"/>
</ReservationControlNumber>
</ReservationControlInformationSegment>
</xsl:template>
</xsl:stylesheet>

Is there a better way of mapping inorder to reduce the size of XSL. Here there are 3 CompanyId and ReservationControlNumber , but it can increase. So our mapping also becomes complex. Please suggest a better approach if any.

Postnext
Tony LavinioSubject: XSL mapping for XMLs having different XML element names in input and output XMLs
Author: Tony Lavinio
Date: 12 Jul 2007 12:30 AM
Unless you want to start matching using
substring(local-name()), you're doing it
properly.

Unflattening arrays, which is essentially
what those segments in EDI are mapped as,
is just plain verbose.

Postnext
James DurningSubject: XSL mapping for XMLs having different XML element names in input and output XMLs
Author: James Durning
Date: 12 Jul 2007 02:08 PM
I'm pretty sure I answered this question before, but to restate and put into context of your current xml:
first to second:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<RCI>
<xsl:for-each select="ReservationControlInformationSegment/CompanyId">
<xsl:variable name="number">
<xsl:number format="00"/>
</xsl:variable>
<xsl:variable name="basename">RCI<xsl:value-of select="$number"/></xsl:variable>
<xsl:element name="{concat($basename, '-ReservationControlInformation')}">
<xsl:element name="{concat($basename, '01-CompanyIdentification')}">
<xsl:value-of select="."/>
</xsl:element>
<xsl:element name="{concat($basename, '02-ReservationControlNumber')}">
<xsl:value-of select="following-sibling::ReservationControlNumber[1]"/>
</xsl:element>
</xsl:element>
</xsl:for-each>
</RCI>
</xsl:template>
</xsl:stylesheet>

Posttop
James DurningSubject: XSL mapping for XMLs having different XML element names in input and output XMLs
Author: James Durning
Date: 12 Jul 2007 02:29 PM
Originally Posted: 12 Jul 2007 02:12 PM
making second to first dynamic is quite a bit uglier:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<ReservationControlInformationSegment>
<xsl:for-each select="*[contains(local-name(), 'ReservationControlInformation')]">
<CompanyId>
<xsl:value-of select="*[contains(local-name(), 'CompanyIdentification'"/>
</CompanyId>
<ReservationControlNumber>
<xsl:value-of select="*[contains(local-name(), 'ReservationControlNumber'"/>
</ReservationControlNumber>
</xsl:for-each>
</ReservationControlInformationSegment>
</xsl:template>
</xsl:stylesheet>

Again, this is best guess based on what you have provided.
If there are multiple repeating nodes inside your other node, (as hinted by your previous topic) then the xslt might have to change somewhat.

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.