tags:

views:

267

answers:

4

Hello,

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"&gt;

  <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 ?

A: 

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

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

</xsl:template>
Dirk
Thank you Dirk. I did not try your anwser, as I started with Tomalak's one, but it seems pretty nice too. I keep the exemple for further use ;-)
Jalil
+1  A: 

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.

Tomalak
Tomalak, thanks for your answer. I tried your suggestion and I had the same error I had with my version : ']' required, '@' found (translated from french):-(
Jalil
Tomalak, I am sorry, I did not tried exactly your solution but directly : <xsl:apply-templates select="export/table[@name='CLIENT']/row[col[@name='PROSPECT' and text() = '1']]"/>I'm going to try your solution as is right now.
Jalil
By the way, please tell the XSLT processor you are using.
Tomalak
That worked ! !Thank you Tomalak !I'm using thx XSLT Processor of the .NET 3.5. By the way, do you guys have a way to test a XSL stylesheet ? Sort of XLST Tester ?
Jalil
In terms of "debugging"? http://msdn.microsoft.com/en-us/library/ms255605%28VS.80%29.aspx
Tomalak
Fantastic ! Thank you !
Jalil
A: 

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

Lloyd
The error I got was : ']' required, '@' found (translated from french)
Jalil
+1  A: 
<xsl:apply-templates select="export/table[@name='CLIENT']/row/col[text()='1' and @name='PROSPECT']"/>
John McG