How to apply an XSL template only to elements containing a child element with a specific 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 pattern (match = "/") only to "strings" that have a perspective value of 1. In my example, this will only transform the first and last strings.
I tried
<xsl:apply-templates select="export/table[@name='CLIENT']/row[col[@name='PROSPECT']=1]"/>
but that gave me a syntax error.
Does anyone know how to proceed?
+2
source to share
4 answers
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>
While your attempt:
<xsl:apply-templates select="
export/table[@name='CLIENT']/row[col[@name='PROSPECT']=1]
"/>
should work too (it's not that obvious, but it's not in itself). Not sure why this doesn't work for you.
+2
source to share