xslt - Exists function in XSL When Statement -
i using xsl when clause transform 1 xml file xml file. need use "exists" function in when test.
here's example source xml:
<people> <person personid="1" location="us" fullname="john doe"/> <person personid="2" location="us" fullname="jane doe"/> </people> <nicknames> <nickname personid="1" nname="johnny d"/> </nicknames> here's example xsl:
<xsl:element name="hasnickname"> <xsl:choose> <!-- if nickname exists in source xml, return true --> <xsl:when test="boolean exists function" <xsl:text>true</xsl:text> </xsl:when> <xsl:otherwise> <xsl:text>false</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:element> can exists part?
suppose variable $personid contains @personid of person checking, checks existence:
<xsl:when test="boolean(//nickname[@personid=$personid]/@nname)"> for similar issues prefer check non-empty/non-whitespace value:
<xsl:when test="normalize-space(//nickname[@personid=$personid]/@nname)!=''"> if use key <xsl:key name="nn" match="//nickname" use="@personid"/>, following possible:
<xsl:when test="normalize-space(key('nn',$personid)/@nname)!=''"> the latter not need $personid variable can directly work @personid of person checking…
Comments
Post a Comment