How can I print a single
without closing it

2019-02-26 02:44发布

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

标签: xslt
4条回答
▲ chillily
2楼-- · 2019-02-26 03:06

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

查看更多
beautiful°
3楼-- · 2019-02-26 03:16

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.

查看更多
Deceive 欺骗
4楼-- · 2019-02-26 03:26

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)

查看更多
Fickle 薄情
5楼-- · 2019-02-26 03:30

This works:

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

Thanks to @Alejandro for the tip in the comments

查看更多
登录 后发表回答