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 April 28th, 2006, 02:49 PM
Registered User
 
Join Date: Apr 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default converting an xml to another xml

Hi!
I have seen "tranforming xml to xml" topic but my problem is little complicated.The X markup needs to be converted into Y markup.
The X markup is as follows:


<class classscheme="SAT">
   <classitem>
     <identifier>
        <name>Sales </name>
               <code>1234</code>
     </identifier>
     <classitem>
        <identifier>
          <name> Tax</name>
          <code>5678</code>
       </identifier>
     <classitem>
       <identifier>
         <name>Items </name>
         <code>9101</code>
       </identifier>
      <classitem>
         <identifier>
        <name>Manufacturing </name>
        <code>1121</code>
         </identifier>
           </classitem>
    </classitem>
     </classitem>
    </classitem>
</class>

The Y Markup is has follows:

     <hier>
          <hierlev role="ancestor">
             <heading>
           <title>Sales</title>
             </heading>
             <hierlev role="ancestor">
                <heading>
                <title> Tax</title>
                </heading>
                <hierlev role="ancestor">
              <heading>
                     <title>Items </title>
               </heading>
                   <hierlev role="ancestor">
                  <heading>
                 <title>Manufacturing </title>
                  </heading>
                      <hierlev role="ancestor">
                  <heading>
                  <title>Revised Code </title>
                   </heading>
               </hierlev>
              <hierlev role="me">
                       <heading>
                    <title> Title Value</title>
                   </heading>
               </hierlev>
                  </hierlev>
               </hierlev>
            </hierlev>
        </hierlev>
    </hier>

The preview screen showing little different format of the Y markup, so the tree structure is as follows:
  Hier
    -Hierlev
        -Heading
           -Title
        -Hierlev
           -Heading
              -Title
           -Hierlev
              -Heading
                 -Title
         so and so

Now two rules for the above conversion:
1)In X markup--The classitem depth is arbitary.
2)In Y Markup--- always the last hierlev element contains two hierlev as children.

I have tried to convert the above conversion but always I am getting following output:
   Hier
     -Hierlev
        -Heading
          -Title
     -Hierlev
        -Heading
          -Title
      so and so
At last I got the answer if the depth is fixed. But I need the way when the classitem depth is arbitary.
Thanks in advance for any suggestion/idea.




 
Old May 1st, 2006, 09:42 PM
Registered User
 
Join Date: Apr 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Please give me a suggestion or idea for my question....because another task also needs same needs.
Thanks in advance.

 
Old May 2nd, 2006, 04:18 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You asked the same question on another forum and you have received responses there telling you that poeple can't work out what your requirements are (which is actually the most common reason for not getting a response - most people don't bother replying if they can't work out what the question is).

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 2nd, 2006, 01:33 PM
Registered User
 
Join Date: Apr 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I will try to explain my question briefly with small sample markup and small xslt code. At the end I will explain what is needed.
Question: Converting an XML markup to another XML Markup Using XSLT.
Sample XML Markup:
<class >
   <classitem>
     <identifier>
        <name>Sales </name>
               <code>1234</code>
     </identifier>
     <classitem>
        <identifier>
          <name> Tax</name>
          <code>5678</code>
       </identifier>
     </classitem>
    </classitem>
</class>

After XSL Transformation, the output markup is as follows:
  <hier>
          <hierlev >
             <heading>
                <title>Sales</title>
             </heading>
             <hierlev>
                <heading>
                    <title> Tax</title>
                </heading>
               </hierlev>
            </hierlev>
    </hier>

The mapping between Input and output markup elements as follows:

class - hier
Classitem - heirlev
identifier - heading
name - title
code has no mapping i.e. the element can be ignored in XSLT Code

I have written XSLT code which works for fixed depth of class.Actually I have written XSLT code for classItem depth of 4, but for simplicity... the following code is for classItem depth of 2 only.
<xsl:template match="class">
        <xsl:param name="count"/>
        <xsl:param name="v1" select="descendant::name[1]/text()"/>
        <xsl:param name="v2" select="descendant::name[2]/text()"/>
        <xsl:call-template name="createHierLevElement">
          <xsl:with-param name="hierLevContent">
           <xsl:element name="heading">
              <xsl:element name="title">
              <xsl:copy-of select="$v1"/>
               </xsl:element>
            </xsl:element>

                <xsl:if test=" $count = 2 ">
          <xsl:call-template name="createHierLevElement">
                      <xsl:with-param name="hierLevContent">
              <xsl:element name="heading">
                      <xsl:element name="title">
                                  <xsl:copy-of select="$v2"/>
                 </xsl:element>
              </xsl:element>
                       </xsl:with-param>
                   </xsl:call-template>
                </xsl:if>
             </xsl:with-param>
        </xsl:call-template>
  </xsl:template>


  <xsl:template name="createHierLevElement">
      <xsl:param name="hierLevContent" />

        <xsl:element name="hierlev">
           <xsl:copy-of select="$hierLevContent" />
        </xsl:element>
    </xsl:template>

If my input markup has arbitary depth, the above XSLT code will not work, so I need code which accomodates any depth of ClassItem.



Thanks.
Sridhar







 
Old May 2nd, 2006, 01:59 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You're raising the same questions on xsl-list. Please stick to one forum at a time.

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
Difficulties converting XML to XML using XSLT Reznik XSLT 7 June 3rd, 2008 05:45 AM
Converting xml to xsl leena XSLT 1 August 3rd, 2007 06:04 AM
Converting Source Xml into Target Xml Using XSL. alapati.sasi XSLT 3 May 14th, 2007 10:54 AM
Converting Exel to XML karephul XML 2 June 14th, 2006 03:27 PM
Converting XML to XML (making element mandatory) boondocksaint20 XSLT 8 April 28th, 2006 10:54 AM





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