How to check if a node element contains a specific value in xsl

I have an XML document:

<?xml version="1.0" encoding="ISO-8859-1"?> <document> <fruits> <fruit id="1"> <title>I like pineapples</title> <description> a tropical plant with edible multiple fruit consisting of coalesced berries</description> </fruit> <fruit id="2"> <title>I like watermelons</title> <description>has a smooth exterior rind (green, yellow and sometimes white) and a juicy, sweet interior flesh</description> </fruit> </fruits> </document> 

How to check if title element contains โ€œpineappleโ€ so that I can display description for this particular fruit ?

Here is the XSLT transformation that I have:

 <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" omit-xml-declaration="yes" doctype-public="-//WAPFORUM//DTD XHTML Mobile 1.0//EN" doctype-system="http://www.wapforum.org/DTD/xhtml-mobile10.dtd"/> <xsl:template match="/"> <xsl:element name="html"> <xsl:element name="head"> <xsl:element name="title">Fruits</xsl:element> </xsl:element> <xsl:element name="body"> <xsl:if test="/document/fruits/fruit/title[contains(text(),'pineapple')]"> <xsl:value-of select="description"/> </xsl:if> </xsl:element> </xsl:element> </xsl:template> </xsl:stylesheet> 
+4
source share
2 answers

Here's a slightly more pushed approach that accomplishes what you want.

When is this XSLT:

 <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output omit-xml-declaration="no" indent="yes" doctype-public="-//WAPFORUM//DTD XHTML Mobile 1.0//EN" doctype-system="http://www.wapforum.org/DTD/xhtml-mobile10.dtd" /> <xsl:strip-space elements="*" /> <xsl:template match="/*"> <html> <head> <title>Fruits</title> </head> <body> <xsl:apply-templates select="fruits/fruit[contains(title, 'pineapple')]" /> </body> </html> </xsl:template> <xsl:template match="fruit"> <xsl:apply-templates select="description" /> </xsl:template> </xsl:stylesheet> 

... applies to the provided XML:

 <?xml version="1.0" encoding="utf-8"?> <document> <fruits> <fruit id="1"> <title>I like pineapples</title> <description>a tropical plant with edible multiple fruit consisting of coalesced berries</description> </fruit> <fruit id="2"> <title>I like watermelons</title> <description>has a smooth exterior rind (green, yellow and sometimes white) and a juicy, sweet interior flesh</description> </fruit> </fruits> </document> 

... the desired result is created:

 <?xml version="1.0"?> <!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>Fruits</title> </head> <body>a tropical plant with edible multiple fruit consisting of coalesced berries</body> </html> 
+6
source

A simpler and almost completely "push style" solution :

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="/*"> <html> <head> <title>Fruits</title> </head> <body><xsl:apply-templates/></body> </html> </xsl:template> <xsl:template match="fruit[contains(title, 'pineapple')]"> <xsl:value-of select="description"/> </xsl:template> <xsl:template match="text()"/> </xsl:stylesheet> 

When this conversion is applied to the provided XML document:

 <document> <fruits> <fruit id="1"> <title>I like pineapples</title> <description> a tropical plant with edible multiple fruit consisting of coalesced berries</description> </fruit> <fruit id="2"> <title>I like watermelons</title> <description>has a smooth exterior rind (green, yellow and sometimes white) and a juicy, sweet interior flesh</description> </fruit> </fruits> </document> 

the desired, correct result is output:

 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Fruits</title> </head> <body> a tropical plant with edible multiple fruit consisting of coalesced berries</body> </html> 
+2
source

All Articles