How can I encapsulate content with some markup, based on a condition? The following code fails because it's invalid xslt. Surely there's an easy way to do this?
By the looks of it, that should work. However is that the "standard way of solving this issue? Potentially the "content" part could be a komplex and large markup that I would not like to duplicate.
You can solve that by using <xsl:call-template name="complexstuff"/> where the complex stuff goes. Then you have a singe template that has your markup, and you just call it when you need it.
As far as a "standard way of doing it", I don't know if there is such a thing. Depends on the exact usecase, and how you like structuring stuff. But it is 'a' way that I have used before.
Because an XSLT stylesheet is an XML file itself, it has to be valid and well-formed (as Morten said), which means that the goodbad old PHP/ASP way of opening a tag in one place while closing it in another, is out of the question. That's actually a good thing - so yes, that is the way you do it - and you can always use separate templates to have it be a non-issue, e.g.:
Conditional markup
How can I encapsulate content with some markup, based on a condition? The following code fails because it's invalid xslt. Surely there's an easy way to do this?
<xsl:if test="1=1">
<div>123
</xsl:if>
content goes here
<xsl:if test="1=1">
456</div>
</xsl:if>
Thanks.
Just to make sure that you get the right answer, I'm guessing that the above example is not exactly what you want? :)
As you say, the above won't work, because it is not a valid xml structure. So a way to do it could be this:
Does that solve your problem?
By the looks of it, that should work. However is that the "standard way of solving this issue? Potentially the "content" part could be a komplex and large markup that I would not like to duplicate.
Works good enough for me here though.
Thanks.
You can solve that by using <xsl:call-template name="complexstuff"/> where the complex stuff goes. Then you have a singe template that has your markup, and you just call it when you need it.
Ok. Thank you.
As far as a "standard way of doing it", I don't know if there is such a thing. Depends on the exact usecase, and how you like structuring stuff. But it is 'a' way that I have used before.
<xsl:text disable-output-escaping="yes" >
<![CDATA[<div>123]]>
</xsl:text>
<xsl:text disable-output-escaping="yes" >
<![CDATA[456</div>]]>
</xsl:text>
Hi Sledger,
Because an XSLT stylesheet is an XML file itself, it has to be valid and well-formed (as Morten said), which means that the goodbad old PHP/ASP way of opening a tag in one place while closing it in another, is out of the question. That's actually a good thing - so yes, that is the way you do it - and you can always use separate templates to have it be a non-issue, e.g.:
/Chriztian
is working on a reply...