For comparing an xml string value against multiple strings, I am doing the following.
<xsl:if test="/Lines/@name = 'John' or /Lines/@name = 'Steve' or /Lines/@name = 'Marc' " >
Can any one tell me, instead of using 'or' in the above case, how can I check whether a string is existing in an set of strings using xslt.
Thanks.
Answer. There is no direct way to compare the dates in XSLT, but the number function we can convert the date into a number and then can perform the comparison in an 'IF' condition. The following function helps to convert the date into number and then can use the >= operator to check the date in an 'IF' condition.
Returns the contents of the current group selected by xsl:for-each-group. Available in XSLT 2.0 and later versions. Available in all Saxon editions. current-group() ➔ item()*
XSLT uses XPath to find information in an XML document. XPath is used to navigate through elements and attributes in XML documents. In the transformation process, XSLT uses XPath to define parts of the source document that should match one or more predefined templates.
XSLT <xsl:text> The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.
Three ways of doing this:
...
<xsl:template match=
"Lines[contains('|John|Steve|Mark|',
concat('|', @name, '|')
)
]
">
<!-- Appropriate processing here -->
</xsl:template>
.2. Test against an externally passed parameter. If the parameter is not externally set, and we are using XSLT 1.0, the xxx:node-set()
extension function needs to be used to convert it to normal node-set, before accessing its children
<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="*"/>
<!-- externally-specified parameter -->
<xsl:param name="pNames">
<n>John</n>
<n>Steve</n>
<n>Mark</n>
</xsl:param>
<xsl:template match="Lines">
<xsl:if test="@name = $pNames/*">
<!-- Appropriate processing here -->
</xsl:if>
</xsl:template>
</xsl:stylesheet>
.3. In XSLT 2.0 compare against a sequence of strings
<xsl:template match="Lines[@name=('John','Steve','Mark')]">
<!-- Appropriate processing here -->
</xsl:template>
XSLT 2.0 only: <xsl:if test="/Lines/@name = ('John', 'Steve', 'Marc')">
With XSLT 1.0 you can't write a literal expression representing a sequence of strings or a set of strings but if you know the literal values then you can construct a set of nodes e.g.
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:data="http://example.com/data"
exclude-result-prefixes="data">
<data:data xmlns="">
<value>John</value>
<value>Steve</value>
<value>Marc</value>
</data:data>
<xsl:variable name="values" select="document('')/xsl:stylesheet/data:data/value"/>
<xsl:template match="...">
<xsl:if test="/Lines/@name = $values">..</xsl:if>
</xsl:template>
</xsl:stylesheet>
Yep - I use substring - put all your name in a string - xsl:variable - then if contains true, the name is there
e.g.
<xsl:variable name="months">**janfebmaraprmajjunjulaugsepoktnovdec</xsl:variable>
<xsl:if test="contains($months,'feb')"> do stuff ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With