So to capture all the data in it, you would do a (xsl: for-each select = "block/if/trans/trans1")
No I wouldn't. I almost never use for-each. I'd do:
<xsl:apply-templates select="block/if/trans/trans1"/>
Now I want to move out of trans and trans1 so I can go into "block/if/if"..
If trans1
is the context node, you can use the ancestor axis to get to its first if
ancestor:
<xsl:template match="trans1">
<xsl:apply-templates select="ancestor::if[1]"/>
</xsl:template>
It's really not clear what you're asking, though. Are you trying to flatten out the hierarchy, and create an HTML table from each block
that has a tr
for each if
, and a td
for each text1
element beneath each if
? If so, that would require something like this:
<xsl:template match="block">
<table>
<xsl:apply-templates select=".//if"/>
</table>
</xsl:template>
<xsl:template match="if">
<tr>
<xsl:apply-templates select="trans/trans1/text1"/>
</tr>
</xsl:template>
<xsl:template match="text1">
<td>
<xsl:value-of select="."/>
</td>
</xsl:template>
In the example above, I've assumed that you know the exact path to the text1
descendant, as shown in your example.
If you don't know the exact path from the if
element to the text1
descendants, it's trickier. You don't want to use
.//text1
because that will select all text1
descendants, including the ones that are descendants of the descendant if
elements. That would result in the same text1
element producing multiple td
elements in the output - one for each if
element it's a descendant of. To avoid this, you need to use a pattern like:
text1 | .//*[name() != 'if']//text1
This selects any immediate text1
children, and also any text1
descendants that are not themselves descendants of descendant if
elements.
Actually, this would also work:
.//text1[ancestor::if[1] = current()]
That finds all text1
descendants whose first if
ancestor, moving up the chain of ancestors, is the current if
element. This is easier to understand than the first example, but the first example probably performs better, unless the XSLT processor is really smart.