How to access an attribute of a node with xslt?

1

Good morning: I hope you can help me I have the following xml:

<?xml version="1.0" encoding="UTF-8"?>
<biblioteca>
  <libro tipo="Novela">
    <titulo>Don quijote</titulo>
    <autor>Miguel de cervantes</autor>
  </libro>
  <libro tipo="teatro">
    <titulo>Romeo y julieta</titulo>
    <autor>William Shakespeare</autor>
  </libro>
  <libro tipo="cuento">
    <titulo>El Aleph</titulo>
    <autor>Jorge Luis Borges</autor>
  </libro>
  <libro tipo="poemas">
    <titulo>Antologia</titulo>
    <autor>Mario Benedetti</autor>
  </libro>
</biblioteca>

And the following transformation xsl: fo:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:template match="stxx">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
            <fo:layout-master-set>
                <fo:simple-page-master master-name="first" 
                                       page-height="29.7cm" 
                                       page-width="21cm" 
                                       margin-top="1cm" 
                                       margin-bottom="2cm" 
                                       margin-left="2.5cm" 
                                       margin-right="2.5cm">
                      <fo:region-body margin-top="3cm"/>
                      <fo:region-before extent="3cm"/>
                      <fo:region-after extent="1.5cm"/>
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="first">
                <fo:flow flow-name="xsl-region-body">
                    <!--fo:table  border-width="0.5mm" border-style="solid"--><!-- Marco para la tabla -->
                    <fo:table>
                        <fo:table-column column-width="3.2cm"/>
                        <fo:table-column column-width="3.2cm"/>
                        <fo:table-column column-width="3.2cm"/>
                        <fo:table-header>
                            <fo:table-row>
                                <fo:table-cell border="solid black 1px" padding="2px"><fo:block>Titulo</fo:block></fo:table-cell>
                                <fo:table-cell border="solid black 1px" padding="2px"><fo:block>Autor</fo:block></fo:table-cell>
                                <fo:table-cell border="solid black 1px" padding="2px"><fo:block>Tipo</fo:block></fo:table-cell>
                            </fo:table-row>                  
                        </fo:table-header>
                        <fo:table-body>
                                <xsl:apply-templates select="biblioteca"/>
                        </fo:table-body>
                    </fo:table>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>
    <xsl:template match="libro">
        <fo:table-row>
            <xsl:apply-templates/>
        </fo:table-row>
    </xsl:template>
    <xsl:template match="titulo">
        <fo:table-cell border="solid black 1px" padding="2px">
            <fo:block><xsl:value-of select="."/></fo:block>
        </fo:table-cell>
    </xsl:template>     
    <xsl:template match="autor">
        <fo:table-cell border="solid black 1px" padding="2px">
            <fo:block><xsl:value-of select="."/></fo:block>
        </fo:table-cell>
    </xsl:template> 

    <xsl:template match="@tipo">
        <fo:table-cell border="solid black 1px" padding="2px">
            <fo:block><xsl:value-of select="."/></fo:block>
        </fo:table-cell>
    </xsl:template>
</xsl:stylesheet>

My question is how can I access the 'type' attribute of the 'book' node since almost any xpath that I place returns empty or thunders?

    
asked by abrahamhs 02.03.2016 в 17:47
source

1 answer

0

You can get the attribute value by:

<xsl:value-of select="biblioteca/libro/@tipo" />

Based on your .xml that's enough to get the value of the attribute tipo .

In your case according to your .xsl you could get the value of the attribute when you access "title", by:

        <fo:block><xsl:value-of select="../@tipo"/></fo:block>

It would stay like this:

<xsl:template match="titulo">
    <fo:table-cell border="solid black 1px" padding="2px">
        <fo:block><xsl:value-of select="."/></fo:block>
    </fo:table-cell>
    <fo:table-cell border="solid black 1px" padding="2px">
        <fo:block><xsl:value-of select="../@tipo"/></fo:block>
    </fo:table-cell>
</xsl:template> 
    
answered by 02.03.2016 / 18:49
source