简体   繁体   中英

Compare values on xslt transform

I am working a XML like this

<!DOCTYPE catalogo SYSTEM "catalogo.dtd">
<catalogo>
    <constelaciones>
        <constelacion situacion="boreal">
            <nombrelatino>Ursa Major</nombrelatino>
            <nombrecomun>Osa Mayor</nombrecomun>
            <abreviatura>UMa</abreviatura>
            <genitivo>Ursae Majoris</genitivo>
            <visibilidad>Circumpolar</visibilidad>
            <imagen>img/uma.jpg</imagen>
        </constelacion>
        <constelacion situacion="boreal">
            <nombrelatino>Andromeda</nombrelatino>
            <nombrecomun>Andromeda</nombrecomun>
            <abreviatura>And</abreviatura>
            <genitivo>Andromedae</genitivo>
            <visibilidad>Otonio</visibilidad>
            <imagen>img/and.jpg</imagen>
        </constelacion>
        <constelacion situacion="austral">
            <nombrelatino>Crux</nombrelatino>
            <nombrecomun>La Cruz del Sur</nombrecomun>
            <abreviatura>Cru</abreviatura>
            <genitivo>Crucis</genitivo>
            <visibilidad>Circumpolar</visibilidad>
            <imagen>img/cru.jpg</imagen>
        </constelacion>
        <constelacion situacion="boreal">
            <nombrelatino>Cygnus</nombrelatino>
            <nombrecomun>El Cisne</nombrecomun>
            <abreviatura>Cyg</abreviatura>
            <genitivo>Cygni</genitivo>
            <visibilidad>Verano</visibilidad>
            <imagen>img/cyg.jpg</imagen>
        </constelacion>
    </constelaciones>

    <objetos>
        <objeto observacion="telescopio" clase="galaxia">
            <codigo>M82</codigo>
            <ubicacion>Osa Mayor</ubicacion>
            <magnitud>8.41</magnitud>
            <imagen>img/m82.jpg</imagen>
        </objeto>
        <objeto observacion="binocular" clase="cumulo">
            <codigo>M45</codigo>
            <denominacion>Pleyades</denominacion>
            <ubicacion>Tauro</ubicacion>
            <magnitud>1.6</magnitud>
            <imagen>img/m45.jpg</imagen>
        </objeto>
            <objeto observacion="telescopio" clase="nebulosa">
            <codigo>NGC6826</codigo>
            <ubicacion>El Cisne</ubicacion>
            <magnitud>10</magnitud>
            <imagen>img/ngc6826.jpg</imagen>
        </objeto>
            <objeto observacion="binocular" clase="galaxia">
            <codigo>M31</codigo>
            <denominacion>Galaxia de Andromeda</denominacion>
            <ubicacion>Andromeda</ubicacion>
            <magnitud>4.36</magnitud>
            <imagen>img/m31.jpg</imagen>
        </objeto>
    </objetos>

    <telescopios>
        <telescopio tipo="refractor">
            <marca>SkyWatcher </marca>
            <modelo></modelo>
            <precio>620</precio>
            <potencia>12.5</potencia>
            <imagen>img/sky.jpg</imagen>
        </telescopio>
        <telescopio tipo="reflector">
            <marca>Orion</marca>
            <modelo>SkyQuest XX12</modelo>
            <precio>1595</precio>
            <potencia>15.1</potencia>
            <imagen>img/orion.jpg</imagen>
        </telescopio>
        <telescopio tipo="binocular">
            <marca>Celestron</marca>
            <modelo>Skymaster 700</modelo>
            <precio>190</precio>
            <potencia>9</potencia>
            <imagen>img/celestron.jpg</imagen>
        </telescopio>
    </telescopios>
</catalogo>

And Xsl stylesheet

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" encoding="utf-8" doctype-system="about:legacy-compat" />
  <xsl:template match="catalogo">
    <html>
      <head>
        <title>Ejercicio 5 Catálogo Estelar</title>
        <link rel="stylesheet" type="text/css" href="css/estilos_catalogo.css"></link>
      </head>
      <body>
        <header>
            <h1>CATÁLOGO ESTELAR</h1>
        </header>
        <xsl:for-each select="constelaciones/constelacion">
            <div>
                <xsl:attribute name="class">constelacion</xsl:attribute>
                <div>
                    <xsl:attribute name="class">descripcion</xsl:attribute>
                    <img>
                        <xsl:attribute name="src">
                            <xsl:value-of select="imagen"/>
                        </xsl:attribute>
                        <xsl:attribute name="alt">
                            <xsl:value-of select="nombrecomun"/>
                        </xsl:attribute>
                        <xsl:attribute name="title">
                            <xsl:value-of select="nombrecomun"/>
                        </xsl:attribute>
                    </img>

                    <h2><xsl:value-of select="nombrelatino"/></h2>
                    <p><xsl:value-of select="nombrecomun"/></p>
                    <p><xsl:value-of select="abreviatura"/></p>
                    <p><xsl:value-of select="genitivo"/></p>
                    <p><xsl:value-of select="visibilidad"/></p>
                </div>
                <div>
                    <xsl:attribute name="class">objetos</xsl:attribute>
                        <xsl:if test="/catalogo/objetos/objeto/ubicacion/text()=nombrecomun/text()">
                            <p><xsl:value-of select="codigo"/></p>
                            <p><xsl:value-of select="magnitud"/></p>
                            <xsl:if test="denominacion">
                                <p><xsl:value-of select="denominacion"/></p>
                            </xsl:if>
                            <img>
                                <xsl:attribute name="src">
                                    <xsl:value-of select="imagen"/>
                                </xsl:attribute>
                                <xsl:attribute name="alt">
                                    <xsl:value-of select="codigo"/>
                                </xsl:attribute>
                                <xsl:attribute name="title">
                                    <xsl:value-of select="codigo"/>
                                </xsl:attribute>
                            </img>
                        </xsl:if>
                </div>
            </div>
        </xsl:for-each>
        <footer>
            <p>José Manuel</p>
            <p>Transformación XSLT</p>
        </footer>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

I need to show all the constellations and also display objects corresponding to each. Comparing <nombrecomun> with <ubicacion> . Objects shouls appear on <div class="objeto"> label. Ive problems with compare between nodes located in differnet elemnts. If i use /catalogo/objetos/objeto/ubicacion/text() , i get always the same value from node. Thanks for the help.

You really should post your expected output here. Minimizing the example would be helpful too.

Now, I suggest you use a key to get the objects related to each constellation. For this, add the following:

<xsl:key name="object" match="objeto" use="ubicacion" />

to the top of your stylesheet, outside of any template. Then change this:

<xsl:if test="/catalogo/objetos/objeto/ubicacion/text()=nombrecomun/text()">
    <p><xsl:value-of select="codigo"/></p>
    <p><xsl:value-of select="magnitud"/></p>
    <xsl:if test="denominacion">
        <p><xsl:value-of select="denominacion"/></p>
    </xsl:if>
    <img>
        <xsl:attribute name="src">
            <xsl:value-of select="imagen"/>
        </xsl:attribute>
        <xsl:attribute name="alt">
            <xsl:value-of select="codigo"/>
        </xsl:attribute>
        <xsl:attribute name="title">
            <xsl:value-of select="codigo"/>
        </xsl:attribute>
    </img>
</xsl:if>

to:

<xsl:for-each select="key('object', nombrecomun)">
    <p><xsl:value-of select="codigo"/></p>
    <p><xsl:value-of select="magnitud"/></p>
    <xsl:if test="denominacion">
        <p><xsl:value-of select="denominacion"/></p>
    </xsl:if>
    <img>
        <xsl:attribute name="src">
            <xsl:value-of select="imagen"/>
        </xsl:attribute>
        <xsl:attribute name="alt">
            <xsl:value-of select="codigo"/>
        </xsl:attribute>
        <xsl:attribute name="title">
            <xsl:value-of select="codigo"/>
        </xsl:attribute>
    </img>
</xsl:for-each>

PS You can shorten your code considerably, if you learn to use the attribute value template .

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