Hello,
I have a document which looks like this:
Code:
<table>
<tr><th>Contact</th><td>Arthur</td></tr>
<tr><th>Phone</th><td>555-LOGS</td></tr>
<tr><th>Contact</th><td>Carrie</td></tr>
<tr><th>Phone</th><td>+4732112330</td></tr>
<tr><th>Email</th><td>[email protected]</td></tr>
<tr><th>Contact</th><td>Doug</td></tr>
</table>
I would like to transform this into something like this:
Code:
<Contacts>
<Contact>
<Name>Arthur</Name>
<Phone>555-LOGS</Phone>
</Contact>
..
.and so on for each contact
</Contacts>
That means I have to select all the elements up until there's an element whitch contains a <th>Contact</th>
This my first try (messy)
Code:
<Contacts>
<xsl:for-each select='//h:table/h:tr/h:th[normalize-space(text())="Contact"]'>
<Contact>
<Name>
<xsl:value-of select='./following-sibling::h:td'/>
</Name>
<xsl:for-each select='./parent::h:tr/following-sibling::h:tr/h:th[normalize-space(text())!="Contact"]/parent::h:tr/following-sibling::h:tr'>
Print out contents of tr...
</xsl:for-each>
</Contact>
</xsl:for-each>
</Contacts>
This kind of worked, but of course all <tr>s without the <th>Contact</th> in them were printed out, instead of just the ones up until the first <tr> with a <th>Contact</th> in it.
This is my second try. (even messier)
The idea is to set a variable TRUE when the parser gets to a <tr><th>Contact</th>.. skip printing the rest of the <tr>s if it has been set. I came up with that half-asleep, so it's not very elegant.
Code:
<Contacts>
<xsl:for-each select='//h:table/h:tr/h:th[normalize-space(text())="Contact"]'>
<Contact>
<Name>
<xsl:value-of select='./following-sibling::h:td'/>
</Name>
<xsl:for-each select='./parent::h:tr/following-sibling::h:tr/h:th'>
<xsl:variable name='trigger'>
<xsl:if test='normalize-space(text())="Contact"'>
TRUE
</xsl:if>
</xsl:variable>
<xsl:if test='not($trigger="TRUE")'>
Print out contents of tr..
</xsl:if>
</xsl:for-each>
</Contact>
</xsl:for-each>
</Contacts>
..this doesn't really work.
Any ideas how I can do this the right way?