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 March 11th, 2010, 09:08 AM
Authorized User
 
Join Date: Mar 2010
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
Default can we create the tags dynamically

Hi,

I have small question to ask ..

can we create the tags dynamically


my sample xml:
================
<sample>
<AA>10</AA>
<AA>20</AA>
<AA>30</AA>
<AA>40</AA>
<AA>30</AA>
<AA>10</AA>
<AA>20</AA>
<AA>20</AA>
<AA>40</AA>
<AA>50</AA>
<AA>60</AA>
<AA>10</AA>
</sample>


i have to writ my xsl so that
the tags names should be like below
for every 4 records the tag name should be same and should increment by 1 for the next record


My out put should be...
=============================

<done>
<1>10</1>
<1>20</1>
<1>30</1>
<1>40</1>
<2>30</2>
<2>10</2>
<2>20</2>
<2>20</2>
<3>40</3>
<3>50</3>
<3>60</3>
<3>10</3>
</done>

Any Suggestions ??

Thanks
Anil
 
Old March 11th, 2010, 09:14 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

With XML a digit is not allowed as the first letter in a name so the output you describe is not even XML.
Other than that you can of course create elements dynamically with computed named using xsl: element.
And you can group the input you have in chunks of four elements, how you do that easily depends on the XSLT version you use. Do you use XSLT 2.0 or 1.0?
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old March 11th, 2010, 09:16 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You can create tags using the <xsl:element> instruction.

However '1' isn't a valid XML element name, as they cannot start with numbers.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old March 11th, 2010, 09:24 AM
Authorized User
 
Join Date: Mar 2010
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Thanks for your quick reply,

iam using XSLT 2.0 version

if numbers isn't a valid XML element name
i will be happy atleast instead of "1" if i get like "AA1"
 
Old March 11th, 2010, 09:32 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

With XSLT 2.0 you can use for-each-group:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  
  <xsl:output indent="yes"/>
  
  <xsl:template match="sample">
    <done>
      <xsl:for-each-group select="AA" group-by="(position() - 1) idiv 4">
        <xsl:for-each select="current-group()">
          <xsl:element name="AA{current-grouping-key() + 1}">
            <xsl:value-of select="."/>
          </xsl:element>
        </xsl:for-each>
      </xsl:for-each-group>
    </done>
  </xsl:template>
 
</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
anarleti (March 13th, 2010)
 
Old March 13th, 2010, 03:17 AM
Authorized User
 
Join Date: Mar 2010
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Hi,

i have got my required out put with the given logic.

Thanks for your time and timely suggestion :)

Regards
Anil





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create rpt file dynamically raghur Crystal Reports 0 August 25th, 2005 11:56 PM
Generating html tags dynamically sachin lad Servlets 1 April 26th, 2005 05:35 PM
Dynamically create tables vickriz Javascript How-To 8 August 5th, 2003 03:00 AM
Dynamically Add input tags Jstmehr4u3 Javascript How-To 2 July 29th, 2003 04:03 AM





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