开发者

How can I print a single <div> without closing it in XSLT

Basically I need to open a div in one if statement and close it in another. I tried

<xsl开发者_JAVA技巧:value-of select="'<div>'"/>

but that failed because < and > aren't allowed in attributes. Any ideas? Cheers


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.


This works:

<xsl:text disable-output-escaping="yes">&lt;div&gt;</xsl:text>

Thanks to @Alejandro for the tip in the comments


If you're just printing it out, you could use the html entities &lt; and &gt; stead of < and >.


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">&lt;div&gt;</xsl:text>

(EDIT: Forgot to add output escaping)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜