Basically I need to open a div in one if statement and close it in another. I tried
<xsl:value-of select="'<div>'"/>
but that failed because < and > aren't allowed in attributes. Any ideas? Cheers
Basically I need to open a div in one if statement and close it in another. I tried
<xsl:value-of select="'<div>'"/>
but that failed because < and > aren't allowed in attributes. Any ideas? Cheers
This is generally bad practice, as you should always open and close the tags of your output at the same level. Otherwise, you are looking at a potential nightmare of "where was I supposed to close this?" questions down the road. That said, this may work:
<xsl:text disable-output-escaping="yes"><div></xsl:text>
(EDIT: Forgot to add output escaping)
If you're just printing it out, you could use the html entities <
and >
stead of <
and >
.
This works:
<xsl:text disable-output-escaping="yes"><div></xsl:text>
Thanks to @Alejandro for the tip in the comments
If what you want to do is output some content regardless of any condition, but wrap the content in a <div>
depending on a condition:
<xsl:choose>
<xsl:when test="myConditionIsTrue">
<div>
<xsl:call-template name="bar"/>
</div>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="bar"/>
</xsl:otherwise>
</xsl:choose>
You can change the <xsl:call-template>
to <xsl:apply-templates>
or <xsl:value-of select="$myvariable" />
etc. depending on what the invariant content is.
This way, you will be treating a tree structure as a tree structure, leveraging the power of an XML tree-based processor, instead of trying to fight against it. DOE may work in many instances, but it's not portable, because XSLT processors are not required to honor it. Indeed they can't, unless they happen to be responsible for serialization in a particular pipeline. The above method avoids this problem.