File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes XSL/XML document structure question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Practical Unit Testing with TestNG and Mockito this week in the Testing forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Reply Bookmark "XSL/XML document structure question" Watch "XSL/XML document structure question" New topic
Author

XSL/XML document structure question

Brendan Kennedy
Ranch Hand

Joined: May 02, 2001
Posts: 65
Does anyone know if there is a way to make XSL read in a node list and
then sequentially go through it(outputting certain things for different
nodes in the order they appear in the xml doc), instead of outputting each
node that is the same together?
Like if you had the following xml:
<textfield>
<textname>firstName</textname>
</textfield>
<combo>
<cmboname>cmboNat</cmboname>
</combo>
<textfield>
<textname>firstName</textname>
</textfield>
the xsl would output a textfield then a combo and then a textfield instead
of two textfields and a combo or vice versa.
Frank Daly
Ranch Hand

Joined: Mar 31, 2000
Posts: 132
Hi Brendan
You should change your xml file to
<control controlType="Text">
<textname>firstName</textname>
</control>
<control controlType="combo">
<cmboname>cmboNat</cmboname>
</control>
<control controlType="Text">
<textname>firstName</textname>
</control>
Your xsl file should then include
<xsl:for-each select="control">
<xsl:choose>
<xsl:when test="@controlType='Text'">
...code to display text box..
</xsl:when>
<xsl therwise>
...code to display combo box...
</xsl therwise>
</xsl:choose>
</xsl:for-eahc>

frank
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
In XSLT there are two approaches to get data from XML document: �pull� and �push�. You pull data when you are using such elements as <xsl:for-each>, <xsl:value-of select>... If, instead, you want to get your data �how they are�, you can write a template for each element you want to see in the output and apply all this templates with <xsl:apply-templates/> element. XSLT processor walks through input tree and for each element applies matching template. Here is your XML document (I added root element and changed content of the last textname to �secondName� with illustrative purpose)
<?xml version="1.0"?>
<root>
<textfield>
<textname>firstName</textname>
</textfield>
<combo>
<cmboname>cmboNat</cmboname>
</combo>
<textfield>
<textname>secondName</textname>
</textfield>
</root>
XSL:
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="textname">
<!-- ... some processing here, like -->
<b><xsl:value-of select="."/></b>
</xsl:template>
<xsl:template match="cmboname">
<!-- ... some other processing here, like -->
<i><xsl:value-of select="."/></i>
</xsl:template>
</xsl:stylesheet>
the output is:
firstName cmboNat secondName

[This message has been edited by Mapraputa Is (edited June 21, 2001).]


Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
 
 
subject: XSL/XML document structure question
 
Threads others viewed
XSL
xslt sorting
xls:sort question
XSL Question
[XSLT] remove a particular node
MyEclipse, The Clear Choice