I want to check occurrence of certain text. Below is my XSLT:
<?xml version="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="/">
<!-- Notify PANTONE - If YES -->
<xsl:if test="contains(xmlreport/PageInfo/PageAttribute/PageColor,'PANTONE')">
<PantonePage><note>Yes</note></PantonePage>
</xsl:if>
<!-- Notify PANTONE - If NO -->
<xsl:if test="not(contains(xmlreport/PageInfo/PageAttribute/PageColor,'PANTONE'))">
<PantonePage><note>No</note></PantonePage>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
My desire output should be either:
<pantone>Yes</pantone>
or
<pantone>No</pantone>
In the original XML, PANTONE might be PANTONE Red U, PANTONE Green U. When tested the above XSLT, the answer is always NO.
Below is my XML:
<?xml version="1.0" encoding="UTF-8"?>
<xmlreport>
<PageInfo>
<PageAttribute>
<PageNum>1</PageNum>
<TrimSize>
<H>220 mm</H>
<W>150 mm</W>
</TrimSize>
<MediaSize>
<H>225 mm</H>
<W>160 mm</W>
</MediaSize>
<PageColor>PANTONE Red U</PageColor>
</PageAttribute>
<PageAttribute>
<PageNum>2</PageNum>
<TrimSize>
<H>220 mm</H>
<W>150 mm</W>
</TrimSize>
<MediaSize>
<H>225 mm</H>
<W>160 mm</W>
</MediaSize>
<PageColor>Black</PageColor>
</PageAttribute>
<PageAttribute>
<PageNum>3</PageNum>
<TrimSize>
<H>220 mm</H>
<W>150 mm</W>
</TrimSize>
<MediaSize>
<H>225 mm</H>
<W>160 mm</W>
</MediaSize>
<PageColor>Cyan Magenta Yellow Black</PageColor>
</PageAttribute>
<PageInfo>
<xmlreport>
The PageAttribute
will be repeated depending on number of pages.
The reason why your attempt doesn't work is that contains()
is a string function and the expression:
contains(xmlreport/PageInfo/PageAttribute/PageColor,'PANTONE')"
tests only the string value of the first node of the set.
Try instead:
<xsl:template match="/">
<PantonePage>
<note>
<xsl:choose>
<xsl:when test="xmlreport/PageInfo/PageAttribute[contains(PageColor, 'PANTONE')]">Yes</xsl:when>
<xsl:otherwise>No</xsl:otherwise>
</xsl:choose>
</note>
</PantonePage>
</xsl:template>
Or - if you can accept a result of true/false instead of Yes/No - simply:
<xsl:template match="/">
<PantonePage>
<note>
<xsl:value-of select="boolean(xmlreport/PageInfo/PageAttribute[contains(PageColor, 'PANTONE')])"/>
</note>
</PantonePage>
</xsl:template>
Edited stylesheet based on provided sample xml
I changed the approach now knowing the business rules more clearly that you expect multiple page attributes within a page info element. Michael's answer definitely accounts for a single value being returns if any of the pages are pantone and is completely correct. The difference in the approach below is that is provides a structure to identify which of the pages are Pantone in case others who stumble on these answers are looking for that business rule by page.
<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="/">
<results>
<xsl:apply-templates />
</results>
</xsl:template>
<xsl:template match="xmlreport/PageInfo/PageAttribute">
<Page>
<PageNum><xsl:number/></PageNum>
<Pantone>
<xsl:choose>
<xsl:when test="contains(PageColor,'PANTONE')">Yes</xsl:when>
<xsl:otherwise>No</xsl:otherwise>
</xsl:choose>
</Pantone>
</Page>
</xsl:template>
</xsl:stylesheet>
Running this against your provided sample XML yields:
<results>
<Page>
<PageNum>1</PageNum>
<Pantone>Yes</Pantone>
</Page>
<Page>
<PageNum>2</PageNum>
<Pantone>No</Pantone>
</Page>
<Page>
<PageNum>3</PageNum>
<Pantone>No</Pantone>
</Page>
</results>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.