Reputation: 622
I've a xml file
<?xml version="1.0" encoding="UTF-8"?>
<CategoriesList>
<Category ID="1" >
<Name ID="1395687" Value="Libelle1" langid="1"/>
<Name ID="1395689" Value="" langid="3"/>
</Category>
<Category ID="2" >
<Name ID="1395687" Value="" langid="1"/>
<Name ID="1395689" Value="Libelle2" langid="3"/>
</Category>
</CategoriesList>
I try to do a smart filter with XSLT file
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="CategoriesList">
<xsl:apply-templates select="Category"/>
</xsl:template>
<xsl:template match="Category">
<xsl:element name="Category">
<xsl:attribute name="ID">
<xsl:value-of select="@ID"/>
</xsl:attribute>
<xsl:apply-templates select="Name"/>
</xsl:element>
</xsl:template>
<xsl:template match="Name[@langid=3]">
<xsl:attribute name="Name">
<xsl:value-of select="@Value"/>
</xsl:attribute>
</xsl:template>
I can filter to take only Name node with langid=3 But I want : if Name node with langid=3 is empty take Name node with langid=1 if Name node with langid=3 is not empty take Name node with langid=3
I saw
Please, can you help me
Upvotes: 0
Views: 94
Reputation: 117140
If "empty" means that the Value attribute is empty, try something like:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/CategoriesList">
<xsl:copy>
<xsl:apply-templates select="Category"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Category">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="Name">
<xsl:choose>
<xsl:when test="Name[@langid=3][string(@Value)]">
<xsl:value-of select="Name[@langid=3]/@Value"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="Name[@langid=1]/@Value"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Applied to your input example, the result is:
<?xml version="1.0" encoding="UTF-8"?>
<CategoriesList>
<Category ID="1" Name="Libelle1"/>
<Category ID="2" Name="Libelle2"/>
</CategoriesList>
Note that we are assuming that langid is unique within each category.
Upvotes: 1