简体   繁体   中英

My XSL in not presenting the data in the xml

I'm trying to do an xsl to present my xml file, but it's not showing the data in the xml file.

XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"
xmlns:datetime="http://exslt.org/dates-and-times">

<xsl:template match="/">
    <html>
        <body>
            <h2>Maçã de Moimenta</h2>

            <div>
                <xsl:for-each select="moimenta/produtores">

                    <!-- 1º tabela -->

                    <table border="1">
                        <tr bgcolor="#9acd32">
                            <th>Relatório: CC / Fornecedor: </th>
                            <th>Data: </th>
                            <th>Ano Fiscal: </th>
                            <th>Colheita: </th>
                            <th>Qualidade da Maça: </th>
                        </tr>

                        <tr>
                            <td/>
                            <td>
                                <xsl:value-of select="produtor/nome"/>
                            </td>
                            <td>
                                <xsl:value-of select="datetime:dateTime()"/>
                            </td>
                            <td>></td>
                            <td>
                                <xsl:value-of select="produtor/colheita/@ano"/>
                            </td>
                            <td>
                                <xsl:value-of select="produtor/colheita/cod_colheita"/>
                            </td>
                            <td>
                                <xsl:value-of select="produtor/colheita/@qualidade"/>
                            </td>
                        </tr>

                    </table>

                    <!-- Tabela Dados da calibragem -->

                    <div> Dados de Calibragem: </div>
                    <div>
                        <table border="1">

                            <tr>
                                <th>Número do Lote:</th>
                                <th>Quantidade Total:</th>
                                <xsl:for-each select="produtor/colheita">
                                    <th>Calibragem: <xsl:value-of
                                            select="valor/preço_valor/@valor"/></th>
                                </xsl:for-each>
                            </tr>

                            <xsl:for-each select="produtor/colheita/lotes">

                                <tr>
                                    <td>
                                        <xsl:value-of select="lote/n_lote"/>
                                    </td>

                                    <xsl:for-each select="lote/calibragem">
                                        <td>
                                            <xsl:value-of select="calib/quantidade"/>
                                        </td>
                                    </xsl:for-each>
                                </tr>

                            </xsl:for-each>

                        </table>

                        <!-- FALTA INFORMAÇÃO SOBRE O QUE FALTA CALIBRAR -->
                    </div>


                    <!-- Tabela Resumo -->

                    <div>Resumo: </div>

                    <table border="1">

                        <tr>
                            <th>Número do Lote:</th>
                            <th>Calibragem:</th>
                            <th>Percentagem:</th>
                        </tr>

                        <xsl:for-each select="produtor/colheita/lotes">

                            <tr>
                                <!-- coloquei 5 porque restringi os valores de calibragem até 4 valores -->
                                <th rowspan="5">
                                    <xsl:value-of select="lote/n_lote"/>
                                </th>
                            </tr>
                            <xsl:for-each select="lote">
                                <tr>
                                    <th>
                                        <xsl:value-of select="calibragem/calib/@valor"/>
                                    </th>
                                    <th><xsl:value-of
                                            select="(calibragem/calib/quantidade div quantidade) * 100"
                                        />%</th>
                                </tr>
                            </xsl:for-each>

                        </xsl:for-each>

                    </table>


                </xsl:for-each>
            </div>
        </body>
    </html>
</xsl:template>

XML File

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="style_macaMoimenta.xsl"?>
<moimenta xmlns="http://www.macamoimenta.pt/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:gnr="http://www.macamoimenta.pt/general" xmlns:clt="http://www.macamoimenta.pt/lote"
xmlns:prc="http://www.macamoimenta.pt/precario"
xmlns:clb="http://www.macamoimenta.pt/calibragem"
xsi:schemaLocation="http://www.macamoimenta.pt/ moimenta.xsd">
<produtores>
    <produtor>
        <codigo>PR001</codigo>
        <nome>Antonio Manuel</nome>
        <data_registo>2018-03-20</data_registo>
        <morada>
            <gnr:rua>Rua Antonio Manuel</gnr:rua>
            <gnr:numero>69</gnr:numero>
            <gnr:cod_postal>4569-123</gnr:cod_postal>
        </morada>
        <colheita ano="2018" qualidade="Ambrosia">
            <cod_colheita>FE23569</cod_colheita>
            <valor>
                <prc:preco_valor valor="0-60">
                    <prc:preco>0.10</prc:preco>
                </prc:preco_valor>
                <prc:preco_valor valor="60-65">
                    <prc:preco>0.13</prc:preco>
                </prc:preco_valor>
                <prc:preco_valor valor="65-70">
                    <prc:preco>0.17</prc:preco>
                </prc:preco_valor>
                <prc:preco_valor valor="DEFEITO">
                    <prc:preco>0.05</prc:preco>
                </prc:preco_valor>
            </valor>
            <lotes>
                <lote>
                    <clt:n_lote>236598</clt:n_lote>
                    <clt:quantidade>2500</clt:quantidade>
                    <clt:calibragem>
                        <clb:calib valor="0-60">
                            <clb:quantidade>1000</clb:quantidade>
                        </clb:calib>
                        <clb:calib valor="60-65">
                            <clb:quantidade>1300</clb:quantidade>
                        </clb:calib>
                        <clb:calib valor="DEFEITO">
                            <clb:quantidade>200</clb:quantidade>
                        </clb:calib>
                    </clt:calibragem>
                </lote>
            </lotes>
        </colheita>
    </produtor>
</produtores>
</moimenta>

Mostly I just need to show some information that it's on the xml in the xsl file. You can understand by the things i am requesting for the xsl file to show with the xml, i dont know why it's not showing what i need. Can you help me please???

You have problems with your namespaces - you don't take them into account.
And you have a problem with your XSLT version:
you are using exslt features in an XSLT-1.0 stylesheet.
You can't do that, because most browsers (unfortunately) only support the strict version 1.0. As a general rule, exslt features are not supported by all browsers, so you have to remove/transform them.

Fixing both problems you XML could look like this (partial response):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:pt="http://www.macamoimenta.pt/"                              <!-- Adding root stylesheet with prefix 'pt' -->
exclude-result-prefixes="xs" version="1.0"
xmlns:datetime="http://exslt.org/dates-and-times">

<xsl:template match="/">
    <html>
        <body>
            <h2>Maçã de Moimenta</h2>    
            <div>
                <xsl:for-each select="pt:moimenta/pt:produtores">   <!-- Use namespaces -->
...
            <td>
              <xsl:value-of select="datetime:dateTime()"/>          <!-- this empression has to be replaced by an XSLT-1.0 compatible version -->
            </td>
...

If you can fix these issues, the stylesheet/XML should work.

XSLT and XQuery share XPath as the expresion language and in My query in not presenting the results of the xml you already learned that a path expression like moimenta/produtores selects elements of those names in no namespace, if your XML didn't have the `xmlns="http://www.macamoimenta.pt/" default namespace declaration.

In XSLT 2 or 3 you have the posibility of setting eg xpath-default-namespace="http://www.macamoimenta.pt/" (see https://www.w3.org/TR/xslt20/#unprefixed-qnames ) to change that.

As with XQuery, if you have input documents with elements from different namespace (like your prc:preco_valor elements) you need to declare them in the XSLT as well to select them in that namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM