Reputation: 17
I'm sorry if this is a duplicate, but I know very little about XSL and so I have failed to apply other answers I have found.
<node>
<xsl:attribute name='TEXT'><xsl:value-of select='@text' /></xsl:attribute>
<richcontent TYPE="NOTE"><xsl:value-of select='@_note' /></richcontent>
</node>
So what I need is to create the <richcontent>
tag only if the @_note
element exists in the original document. If it is absent, I don't want to create the <richcontent>
tag in the new document.
I suspect I need something like "when", but I'm not sure how to use it. When I try the following
<node>
<xsl:when test='@_note'>
<richcontent TYPE="NOTE"><xsl:value-of select='@_note' />
</richcontent>
</xsl:when>
</node>
I get this error
element when is not allowed within that context
Upvotes: 0
Views: 413
Reputation: 29042
There can be no @_node
element, because this is not a valid QName, because it starts with the '@' character. QNames can only start with :
, _
, A-Z
, a-z
, or some UniCode characters described in the link above.
Your <xsl:when>
approach to a solution is invalid, because <xsl:when>
is only valid in the context of <xsl:choose>
like this:
<node>
<xsl:choose>
<xsl:when test='@_note'>
<richcontent TYPE="NOTE">
<xsl:value-of select='@_note' />
</richcontent>
</xsl:when>
</xsl:choose>
</node>
But you need to rename your element @_note
to something that conforms to the QName specification.
Upvotes: 0
Reputation: 117073
The error you see is thrown because xsl:when
must be a child of xsl:choose
.
In your case, when you have no alternative xsl:when
or xsl:otherwise
instructions, you should be using xsl:if
instead.
Alternatively (and perhaps preferably), you could use a template matching the node in question: if the node does not exist, the template will not be applied, and the new element will not be created.
Upvotes: 2