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 May 31st, 2007, 11:59 PM
Registered User
 
Join Date: Apr 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default adding tags to output

is it possible to add a start and end tag to the output using XSLT?
note that I want to add the two tags (start and end) at two different pieces of XSL(say using two different templates). So I am not adding this element at one place but need to just put tags in output that would later resolve to a structurally correct xml. Can I do this?

 
Old June 1st, 2007, 01:27 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

No, for two reasons, firstly the XSLT has to be XML, so nested elements etc. Secondly XSLT has no concept of tags, it deals in nodes and you can't output half a node.
You need to rethink the design somewhat, from a procedural to functional and declarative, if you have a specific example post the XML and XSLT.

--

Joe (Microsoft MVP - XML)
 
Old June 1st, 2007, 03:42 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The output of a transformation is a tree containing nodes. Don't even think about tags, leave that to the serializer.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 1st, 2007, 07:56 AM
Registered User
 
Join Date: Apr 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In xml I would get elements such as
<fieldgroup class="start" id="x"/>
<field type="text" size... value=.../>
...other fields..
<fieldgroup class="end"/>

Basically the xml tells me a group of fields wrapped by a pair of fieldgroup elements. so each field would be placed individually inside <tr><td> which I can do while looping and when come across each field.
When I see fieldgroup with class="start" I need to just place <tbody> tag with the id from xml. And when seen fieldgroup with class="end" I need to put closing tag </tbody>. Note that the xml could even have <fieldgroupstart> and <fieldgroupend> alternatively but it's still the same effect. The issue is that getting the proper xml such as <fieldgroup>
 <field>....</field>
 ....
</fieldgroup> right now is very complicated. So this is a workaround for short time till we revamp the xml getter piece. I was trying to see if I can get the xsl to do what I want: which is while in a loop, see xml elements and place start and end tags of tbody; in case of fields place entire content element e.g. <tr><td><input.../></td></tr>. The problem comes because I see fieldgroup start and end in two different iterations of a loop and so I don't get a chance to place <tbody> element but have to place start and end tag. Has anyone done something like this before? and can I even possibly do it?
Thanks for responses!



 
Old June 1st, 2007, 11:51 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This kind of problem is known as positional grouping and it can be quite tricky, especially in XSLT 1.0 - it's usually much easier in 2.0. Google for "positional grouping XSLT" for some background. Your thinking, of working at the level of tags, is understandable, and you can implement solutions that way in other languages, but it's not the XSLT way.

The first point about such problems is to design your stylesheet around the output structure, not the input structure. Your remarks such as "which I can do while looping and when come across each field" suggest that your code is following the input structure. You say it yourself: "The problem comes because I see fieldgroup start and end in two different iterations of a loop". You need to invert the structure.

Draw the structure of your result tree. Write templates to generate each element in the result tree. Then fill in the templates by asking "what information do I need to fetch from the source document to fill in the content of this element?"

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
adding prefix to nodes and subnodes to output xml raghurns XSLT 9 November 17th, 2006 04:41 PM
XSLT output with tags trinkets XSLT 22 November 14th, 2006 11:22 AM
adding word wrap to test output to a report hbenditsky Visual Basic 2005 Basics 0 February 5th, 2006 06:46 PM
Adding a <p> in text that has no xml tags dzisaacs XSLT 1 November 1st, 2005 02:14 PM
Adding dropdownlists to datalist output nordestgaard .NET Web Services 0 July 27th, 2003 07:24 AM





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