XML to XSLT can open data from xml -
i have 2 files
xml file:
<cadeiahoteis> <hoteis id="1"> <codigo>458</codigo> <morada>porto</morada> <num_quartos>3</num_quartos> <piscina>não</piscina> <restaurante> <dados>sim</dados> <num_mesas>10</num_mesas> <num_pessoas>3</num_pessoas> <hora_abertura>11:30</hora_abertura> <hora_fechar>23:00</hora_fechar> </restaurante> <num_funcionarios>8</num_funcionarios> </hoteis> <hoteis id="2"> <codigo>381</codigo> <morada>lisboa</morada> <num_quartos>25</num_quartos> <piscina>sim</piscina> <restaurante> <dados></dados> <num_mesas></num_mesas> <num_pessoas></num_pessoas> <hora_abertura></hora_abertura> <hora_fechar></hora_fechar> </restaurante> <num_funcionarios>6</num_funcionarios> </hoteis>
xslt file:
<xsl:template match="ns:hoteis_"> <xsl:variable name="hoteis/id" select="@id_hotel"/> <xsl:for-each select="//hoteis/restaurante"> <xsl:if test="dados != ' '"> <tr> <td align="center"> <xsl:value-of select= "ns:num_mesas" /> </td> <td align="center"> <xsl:value-of select= "ns:num_pessoas" /> </td> <td align="center"> <xsl:value-of select= "ns:hora_abertura" /> </td> <td align="center"> <xsl:value-of select= "ns:hora_fechar" /> </td> </tr> </xsl:if> <xsl:if test="not(string(dados))"> <tr> <td>dont exit restaurant <xsl:value-of select= "@id_hotel" /> </td> </tr> </xsl:if> </xsl:for-each> </xsl:template>
i want t restaurant data if dados not empty not show, know how can show data??
since source elements unprefixed , there prefixed element selectors in xslt, i'm assuming all elements belong same namespace (which called your-namespace
) , source starts this:
<cadeiahoteis xmlns="your-namespace"> <hoteis id="1"> ...
if that's not case, have make adjustments stylesheet below.
considering elements belonging namespace, stylesheet should declare , map prefix (ns
). should use prefix qualify element selectors (or else not found, , none of tests work). finally, can test empty dados
element normalize-space(ns:dados)
. better inside xs:choose
block since can treat other case in xs:otherwise
.
this stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:ns="your-namespace" version="1.0"> <xsl:strip-space elements="*"/> <xsl:output indent="yes"/> <xsl:template match="ns:cadeiahoteis"> <table> <xsl:apply-templates/> </table> </xsl:template> <xsl:template match="ns:hoteis"> <xsl:variable name="hoteis_id" select="@id"/> <xsl:for-each select="ns:restaurante"> <xsl:choose> <xsl:when test="normalize-space(ns:dados)"> <tr> <td align="center"> <xsl:value-of select= "ns:num_mesas" /> </td> <td align="center"> <xsl:value-of select= "ns:num_pessoas" /> </td> <td align="center"> <xsl:value-of select= "ns:hora_abertura" /> </td> <td align="center"> <xsl:value-of select= "ns:hora_fechar" /> </td> </tr> </xsl:when> <xsl:otherwise> <tr> <td>dont exit restaurant <xsl:value-of select= "@id_hotel" /> </td> </tr> </xsl:otherwise> </xsl:choose> </xsl:for-each> </xsl:template> </xsl:stylesheet>
applied input data (with namespace attribute added above), produce result:
<?xml version="1.0" encoding="utf-8"?> <table xmlns:ns="your-namespace"> <tr> <td align="center">10</td> <td align="center">3</td> <td align="center">11:30</td> <td align="center">23:00</td> </tr> <tr> <td>dont exit restaurant </td> </tr> </table>
you can test , verify result here.
Comments
Post a Comment