Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 28th, 2005, 01:35 PM
Registered User
 
Join Date: Dec 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Xml to XML using XSLT (urgent)

Hi Group,


I am working on xslt to transform an XML file to create another XML
file using XSLT,


my in put is an xml file like this


myInput.xml


<?xml version="1.0" encoding="utf-8" ?>
<Doc>
                <DocId><Dfx>1.0</Dfx></DocId>
                <DocId><Dfx>1.2</Dfx></DocId>
                <DocId><Dfx>1.01</Dfx></DocId>
                <DocId><Dfx>1.1</Dfx></DocId>
                <DocId><Dfx>1.11</Dfx></DocId>


</Doc>


and output will be all sorted and arranged order like this


myOutput.xml


<?xml version="1.0" encoding="utf-8" ?>
<Doc>
<Dfx>1.0
         <Dfx>1.01</Dfx>
         <Dfx>1.1
                 <Dfx>1.11</Dfx>
         </Dfx>
         <Dfx>1.2</Dfx>
     </Dfx>


What it means is my output xml file like tree structure


but i am not getting properly,,


i have written XSLT for this and please check it and give me the exact
solution for this


my xslt is like this


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="/">
                <xsl:element name="Doc">
                        <xsl:for-each select="/Doc/DocId">
                                <xsl:sort select="Dfx" />
                                <xsl:variable name="zeroVersion" select="Dfx" />
                                <xsl:choose>
                                <xsl:when test="contains($zeroVersion, '.0')">
                                        <xsl:element name="Dfx">
                                                <xsl:value-of select="$zeroVersion" />
                                                         <xsl:call-template name="SubDfx">
                                                                <xsl:with-param name="version"
select="concat(substring-before($zeroVersion,'.'),'.')" />
                                                        </xsl:call-template>
                                        </xsl:element>


                                </xsl:when>
                                <xsl:otherwise>
                                <!-- <xsl:if test="contains($zeroVersion, '.0')">
                                        <xsl:element name="Dfx">
                                                <xsl:value-of select="$zeroVersion" />
                                                         <xsl:call-template name="SubDfx">
                                                                <xsl:with-param name="version"
select="concat(substring-before($zeroVersion,'.'),'.')" />
                                                        </xsl:call-template>
                                        </xsl:element>
                                </xsl:if> -->
                                </xsl:otherwise>
                                </xsl:choose>
                        </xsl:for-each>
                </xsl:element>
        </xsl:template>


        <xsl:template name="SubDfx">
                <xsl:param name="version" />
                <xsl:for-each select="/Doc/DocId">
                        <xsl:sort select="Dfx" />
                        <xsl:variable name="subVersion" select="Dfx" />
                                <xsl:if test="number($subVersion) > number($version)" >
                                <xsl:if test="starts-with($subVersion,$version)">
                                        <xsl:element name="Dfx">
                                                <xsl:value-of select="$subVersion" />
                                                        <xsl:call-template name="SubDfx">
                                                                <xsl:with-param name="version" select="$subVersion" />
                                                        </xsl:call-template>
                                        </xsl:element>
                                 </xsl:if>
                        </xsl:if>
                </xsl:for-each>
        </xsl:template>




</xsl:stylesheet>




naveen sreepada
 
Old December 30th, 2005, 01:48 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The approach to this problem is:

(a) in the root template, apply-templates to the highest-level DfX element

(b) in the template with match="Dfx", apply-templates to the "logical children" of this Dfx element.

In your case, the highest-level element is the one numbered 1.0, and the logical children of an element are the ones whose number (a) starts with the parent's number, and (b) is one digit longer than the parent's number, ignoring any trailing "0" in the parent's number.

In 2.0 that looks like

<xsl:template match="Dfx">
  <xsl:variable name="num" select="replace(., '0$', '')"
  <Dfx>
    <xsl:apply-templates select="../Dfx[
        string-length(.) = string-length($num + 1) and
        starts-with(., $num)"/>
  </Dfx>
</xsl:template>

In 1.0 you have to replace the "replace" function with something involving substring().



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Urgent help regarding XSLT parsing.... to xml.. netbramha XSLT 1 September 19th, 2005 09:03 AM
xml and xsl templates as input to xslt gives xml rameshnarayan XSLT 5 August 3rd, 2005 01:58 AM
XSLT for complicated xml to xml transf. required doug@sirvisetti XSLT 3 June 17th, 2005 04:26 PM
merge two xml file and make new xml using xslt ketan XSLT 0 September 21st, 2004 08:48 AM
Merge XML files into a xml file using xslt lxu XML 4 November 6th, 2003 06:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.