'How to check for atomic item in XPATH?
i would like to check a sequence of Items and report the item type:
- If it is an atomic value (maybe an empty string) report String "A"
- If it is an Element report its local Name
- Otherwise report String "X"
My Question is: How can i check an item to be an atomic value? The only Solution that i found is to use the xs:string(.) function, which takes an anyAtomic as argument. See the following XSLT 3.0 fragment
<xsl:template match="/*">
<xsl:variable name="content" as="item()*">
<xsl:apply-templates select="node()"/>
</xsl:variable>
<result>
<xsl:for-each select="$content">
<xsl:choose>
<!-- I`d like to check for any atomic value here -->
<xsl:when test="xs:string(.)">A</xsl:when>
<xsl:when test="self::*"><xsl:value-of select="lower-case(local-name())"/></xsl:when>
<xsl:otherwise>X</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</result>
</xsl:template>
<xsl:template match="*">
<xsl:copy/>
</xsl:template>
<xsl:template match="text()" as="xs:string">
<!-- What if i change to normalize-space(.) ? -->
<xsl:sequence select="."/>
</xsl:template>
Problem is, that i get an error when i use the normalize-space() function in the template matching text-Node, because in this case the effective boolean value of xs:string() is false for some text Nodes. The following check for the self Axis fails with the error message XPTY0020: The required item type of the context item for the self axis is node(); the supplied value "" is an atomic value.
I have tried with expressions like ". is anyAtomic", but the IS Operator expects Nodes.
I am pretty sure that xs:string(.) is not the correct way to check for atomic items, but what is it?
Thanks, Frank Steimke
Solution 1:[1]
XPath has an instance of operator you can use with sequence types so I think you are looking for . instance of xs:anyAtomicType or e.g. . instance of xs:string.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
