Alpha2k
Alpha2k

Reputation: 2241

XSLT Copying text from label

Got this XML

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="albaran.xsl"?>
<albaran>
   <articulo precio="1.23" unidades="3">Tornillos</articulo>
   <articulo precio="2.34" unidades="5">Arandelas</articulo>
   <articulo precio="3.45" unidades="7">Tuercas</articulo>
   <articulo precio="4.56" unidades="9">Alcayatas</articulo>
</albaran>  

The XSLT output must be an attribute called "total" which multiplies "unidades" * "precio". So the output must be for example:

 <articulo total="3.69">Tornillos</articulo>

But i cant manage to copy the Text inside "articulo", i get "Tornillos" every time... This is my XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="albaran">
<xsl:copy>
  <xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="articulo">
<xsl:copy>
  <xsl:attribute name="total">
    <xsl:value-of select="@precio * @unidades"/>
  </xsl:attribute>
  <xsl:value-of select="//articulo"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

Upvotes: 0

Views: 52

Answers (2)

Linga Murthy C S
Linga Murthy C S

Reputation: 5432

You can use this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="albaran">
    <xsl:copy>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

<xsl:template match="articulo">
    <xsl:copy>
        <xsl:attribute name="total">
            <xsl:value-of select="@precio * @unidades"/>
        </xsl:attribute>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Upvotes: 0

Jason Aller
Jason Aller

Reputation: 3652

<xsl:value-of select="//articulo"/> is selecting:

// from anywhere

articulo grab the first articulo

Change to <xsl:value-of select="."/> which is value of current scope. The current scope was already set by the match portion of the xsl:template.

Upvotes: 1

Related Questions