0

I'm trying to write a XSLT1.0 template that accepts a node as parameter. Inside that template, I need to test if the node passed as parameter is of particular type, in my case a text node. I can check the type of the current node via self::text() and similar constructs, but how do I do that when the node in question is given by a variable?

Here's a piece of code that actually does what I need, but I think there must be a more straightforward way to achieve that. This $node/../text() does not seem right to me, to say the least.

<xsl:template name="renderCommand">
    <xsl:param name="node"/>

    <xsl:variable name="nodeName">
        <xsl:choose>
            <xsl:when test="$node/../text()">
                <xsl:value-of select="name($node)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="concat('.', name($node))"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
</xsl:template>
Eduard Malakhov
  • 1,095
  • 4
  • 13
  • 25
  • 1
    Based on your verbal description I would say that simply `when="$node/self::text()"` should do but looking at your code I wonder what you expect to get as the name of the text node and of course you have not shown at all the code where you pass the node. – Martin Honnen Mar 07 '18 at 09:15
  • @MartinHonnen You're right, `$node/self::text()` does the trick, thank you. Getting the name for a text node makes no sense, indeed. I didn't think about this when I made up this example; the actual code is a bit different. – Eduard Malakhov Mar 07 '18 at 09:24
  • I have put the suggestion from the comment into an answer so that you can mark your question as being solved. – Martin Honnen Mar 07 '18 at 09:38
  • @MartinHonnen sure, thanks again) – Eduard Malakhov Mar 07 '18 at 10:10

1 Answers1

1

If you have a variable representing a node and want to check it is a text node then using when="$node/self::text()" suffices.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • But note that checking whether the node is an attribute is more tricky. – Michael Kay Mar 07 '18 at 12:09
  • @MichaelKay you mean the `count(.|../@*)=count(../@*)` trick? By the way, I disagree that this is a duplicate of [that question](https://stackoverflow.com/questions/14118670/check-type-of-node-in-xsl-template) because my point was about turning a variable that represents a node into something that I can use `::text()` with. I have of course read that question and the answers to it, but they only use `self`directly. – Eduard Malakhov Mar 07 '18 at 12:39
  • XPath is a pretty orthogonal language, so if you've got code that works on the context node, it's trivial to convert it to code that works on a variable. – Michael Kay Mar 07 '18 at 15:36
  • @MichaelKay The problem here is that to google this kind of stuff you need to formulate the question right, which is a tough task if you don't even know how things are properly called. For example, what is that `text()` - a function? And `self::` - an operator? In this sense XPath and XSLT are very specific languages, I can't even think of any other examples of "transformation" languages of this kind. – Eduard Malakhov Mar 09 '18 at 15:43
  • E.g. all OO languages share common concepts so once you know one language, it is fairly straightforward to find how to do the same thing in another language.But with XPath, the only option is to learn XPath. No knowledge of a different language will help you. – Eduard Malakhov Mar 09 '18 at 15:43
  • @EduardMalakhov marking a question as a duplicate is in no way a criticism of the person who asked the question. It's done because having one high-quality answer for multiple questions is better than having multiple answers. However, I do strongly recommend that before you start coding in any language, you take the time to learn its terminology. Otherwise, how can you ever understand an error message? – Michael Kay Mar 09 '18 at 16:38
  • @MichaelKay I do not treat this as criticism, I just think that from the perspective of the common good it is nice to have this specific question answered too because someone else who faces the same issue might find this answer and save some time. – Eduard Malakhov Mar 09 '18 at 17:20
  • In the ideal world, I would agree with you - it is better to take the time to learn some basics. But in reality, nowadays developers often need to switch technologies so frequently that there's practically no time to learn - you just have to start coding. Or, like in my case with XPath, one uses a language so rarely that the time spent on thorough learning simply doesn't pay off. – Eduard Malakhov Mar 09 '18 at 17:20