Jalil
Jalil

Reputation: 3178

How to apply a XSL template only on elements containing a child with a particular attribute value and element value?

I have the following XML (partial) document :

<export>
<table name="CLIENT">
    <row>
        <col name="CODE_CLIENT" type="System.String">1000010026</col>
       <col name="LIBELLE" type="System.String">Test|</col>
        <col name="PROSPECT" type="System.Decimal">1</col>
    </row>
    <row>
        <col name="CODE_CLIENT" type="System.String">1000010025</col>
        <col name="LIBELLE" type="System.String">Rue de la 2eme ad|</col>
        <col name="PROSPECT" type="System.Decimal">0</col>
    </row>
    <row>
        <col name="CODE_CLIENT" type="System.String">1000010125</col>
       <col name="LIBELLE" type="System.String">Test4</col>
        <col name="PROSPECT" type="System.Decimal">0</col>
    </row>
    <row>
        <col name="CODE_CLIENT" type="System.String">1000010035</col>
        <col name="LIBELLE" type="System.String">Rue</col>
        <col name="PROSPECT" type="System.Decimal">1</col>
    </row>
    </table></export>

and the following XSL :

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

  <xsl:output method="text" indent="yes"/>

  <xsl:template match="/">
    <xsl:apply-templates select="export/table[@name='CLIENT']"/>

  </xsl:template>

  <xsl:template match="row">
        SOME TEMPLATE CODE

  </xsl:template>


</xsl:stylesheet>

I would like to apply the first template (match="/") only to the "rows" that have prospect value to 1. In my exemple that would only transform the first and last rows.

I tried

<xsl:apply-templates select="export/table[@name='CLIENT']/row[col[@name='PROSPECT']=1]"/>

but that gave me a syntax error.

Anyone knows how to proceed ?

Upvotes: 2

Views: 3087

Answers (4)

John McG
John McG

Reputation: 698

<xsl:apply-templates select="export/table[@name='CLIENT']/row/col[text()='1' and @name='PROSPECT']"/>

Upvotes: 0

Tomalak
Tomalak

Reputation: 338336

My suggestion:

<xsl:stylesheet 
  version="1.1" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:output method="text" indent="yes"/>

  <xsl:template match="/">
    <xsl:apply-templates select="export/table[@name='CLIENT']"/>
  </xsl:template>

  <xsl:template match="table">
    <xsl:apply-templates select="row[col[@name='PROSPECT' and text() = '1']]" />
  </xsl:template>

  <xsl:template match="row">
    SOME TEMPLATE CODE
  </xsl:template>

</xsl:stylesheet>

Though your try:

<xsl:apply-templates select="
  export/table[@name='CLIENT']/row[col[@name='PROSPECT']=1]
"/>

should work as well (it's not as obvious, but it is not wrong per se). Not sure why it does not work for you.

Upvotes: 2

Lloyd
Lloyd

Reputation: 1324

I tried your apply-templates and it didn't fail. Are you sure error is in the apply-templates?

Upvotes: 0

Dirk
Dirk

Reputation: 31061

Careful: entriely untested. It might not even parse correctly.

<xsl:template match="row[string(./col[@name='PROSPECT']) = '1']">

</xsl:template>

Upvotes: 0

Related Questions