Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT with unknown node names

Status
Not open for further replies.

mooksbc

Programmer
May 13, 2009
5
GB
I have XML files in the following format
<FormData>
<fieldgroup1>
<field1>data</field1>
<field2>data<field2>
</fielggroup1>
<fieldgroupXX>
<field1>data</field1>
<field2>data<field2>
</fielggroupXX>
...
</FormData>

I dont know the names of the fieldgroups or fields beforehand, but these child nodes are alway in parent FormData

I want to create the HTML output

<h1>fieldgroup</h1>
<table>
<tr><td>field</td><td>data</td></tr>
</table>
... etc (for each fieldgroup)

I'm sure this will be very straightforward but I am very new to XSLT and can't find an examplr equivalent to waht I'm attempting. Only examples with for-each and template mathes where the nodes can be referenced directly.

Thanks
 
The three levels of elements are matched by these.
[tt]
<xsl:template match="FormData">
<!-- etc etc -->
</xsl:template>
<xsl:template match="FormData/*">
<!-- etc etc -->
</xsl:template>
<xsl:template match="FormData/*/*">
<!-- etc etc -->
</xsl:template>[/tt]
 
What have you done with it then, or any other thing else at all?
 
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:transform version="1.0" xmlns:xsl=" <xsl:template match="/">
<xsl:for-each select="FormData/*">
<h1><xsl:value-of select="name()"/></h1>
<table>
<xsl:for-each select="FormData/*/*">
<tr>
<td><xsl:value-of select="name()"/></td><td><xsl:value-of select="name()"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
</xsl:template>
</xsl:transform>

the nested for each loop isn't working as i expected.
the output is a list of fieldgroups followed by blank table tags, then the list of fields ... but getting there slowly and painfully
 
><xsl:for-each select="FormData/*/*">
Because you are already in the context of FormData/*, it is this.
[tt]<xsl:for-each select="*">[/tt]
 
Perfect. Thankyou!

One last thing ... can I reference the full path of the node ie i'd like the output in one of the columns to be FormData/fieldgroup/fieldname
 
Sure.
[tt]
<xsl:for-each select="FormData/*">
[blue]<xsl:variable name="lev0" select="'FormData'" />
<xsl:variable name="lev1" select="name()" />[/blue]
<h1><xsl:value-of select="name()"/></h1>
<table>
<xsl:for-each select="*">
[blue]<xsl:variable name="lev2" select="name()" />[/blue]
<tr>
[blue]<td><xsl:value-of select="concat($lev0,'/',$lev1,'/',$lev2)"/></td>[/blue]
<td><xsl:value-of select="name()"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top