Jump to content

How do i edit this to add 2 temples "match"?


saralima

Recommended Posts

Arghhh driving me nuts. <?xml version="1.0" encoding="ISO-8859-1"?><!-- Edited by XMLSpy® --><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"> <html> <body> <h2>Arbol genealogico</h2> <table border="1" width="243"> <tr bgcolor="blue" > <th>Familia</th> </tr></table><table border="1" width="243"> <tr bgcolor="red" > <th>Primos</th> </tr></table> <table border="1"> <tr bgcolor="#9acd32"> <th>Nombre</th> <th>Apellido</th> <th>Fechanac</th> <th>Edad</th> <th>Att->Rama</th> </tr> <xsl:for-each select="Arbolgenealogico/Familia/Primos"> <tr> <td><xsl:value-of select="Nombre"/></td> <td><xsl:value-of select="Apellido"/></td> <td><xsl:value-of select="Fechanac"/></td> <td><xsl:value-of select="Edad"/></td> <td><xsl:value-of select="@Rama"/></td> </tr> </xsl:for-each> </table><table border="1" width="243"> <tr bgcolor="#grey" > <th>Abuelos</th> </tr></table> <table border="1"> <tr bgcolor="#9acd32"> <th>Nombre</th> <th>Apellido</th> <th>Fechanac</th> <th>Edad</th> </tr> <xsl:for-each select="Arbolgenealogico/Familia/Abuelos/Abuela"> <tr> <td><xsl:value-of select="Nombre"/></td> <td><xsl:value-of select="Apellido"/></td> <td><xsl:value-of select="Fechanac"/></td> <td><xsl:value-of select="Edad"/></td> </tr> </xsl:for-each> </table><table border="1" width="243"> <tr bgcolor="#brown" > <th>Tios</th> </tr></table> <table border="1"> <tr bgcolor="#9acd32"> <th>Nombre</th> <th>Apellido</th> <th>Fechanac</th> <th>Edad</th> </tr> <xsl:for-each select="Arbolgenealogico/Familia/Tios/Tio"> <tr> <td><xsl:value-of select="Nombre"/></td> <td><xsl:value-of select="Apellido"/></td> <td><xsl:value-of select="Fechanac"/></td> <td><xsl:value-of select="Edad"/></td> </tr> </xsl:for-each> </table> </body> </html></xsl:template></xsl:stylesheet> I did it like this from an xml and now the the project specifies that i add 2 temple match... i have no idea, ive read a lot and cant make it work

Link to comment
Share on other sites

To give you an example, instead of putting all code into one template, your code is easier to read and maintain if you replace stuff like

<xsl:for-each select="Arbolgenealogico/Familia/Primos"><tr><td><xsl:value-of select="Nombre"/></td><td><xsl:value-of select="Apellido"/></td><td><xsl:value-of select="Fechanac"/></td><td><xsl:value-of select="Edad"/></td><td><xsl:value-of select="@Rama"/></td></tr></xsl:for-each>

by

<xsl:apply-templates select="Arbolgenealogico/Familia/Primos"/>

and then write a template

<xsl:template match="Familia/Primos">   <tr>	 <td><xsl:value-of select="Nombre"/></td>    <td><xsl:value-of select="Apellido"/></td>    <td><xsl:value-of select="Fechanac"/></td>    <td><xsl:value-of select="Edad"/></td>    <td><xsl:value-of select="@Rama"/></td>   </tr></xsl:template>

You might even go further and use

<xsl:template match="Familia/Primos">   <tr>	  <xsl:apply-templates select="Nombre , Apellido , Fechanac , Edad , @Rama"/>   </tr></xsl:template><xsl:template match="Nombre | Apellido | Fechanac | Edad | Primos/@Rama">   <td>	  <xsl:value-of select="."/>   </td></xsl:template>

However

<xsl:apply-templates select="Nombre , Apellido , Fechanac , Edad , @Rama"/>

is only possible with XSLT 2.0 (and XSLT 2.0 processors like Saxon 9 or AltovaXML or XmlPrime), for XSLT 1.0 you could do

<xsl:template match="Familia/Primos">   <tr>	  <xsl:apply-templates select="@Rama | *"/>   </tr></xsl:template>

but then the order in which columns are output depends on the order of the child elements in the input.If that order is different from the output order you want then with XSLT 1.0 you need to spell out the order with

<xsl:template match="Familia/Primos">   <tr>	  <xsl:apply-templates select="Nombre"/>	  <xsl:apply-templates select="Apellido"/>	  <xsl:apply-templates select="Fechanac"/>	  <xsl:apply-templates select="Edad"/>	  <xsl:apply-templates select="@Rama"/>   </tr></xsl:template>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...