Using xsl:if doesn't include closing tag

柔情痞子 提交于 2019-12-08 04:52:55

问题


Trying to use the following code to insert a google ad inside of a blog roll

<xsl:if test="position() = 3">
    <object data="/frontpage_blogroll_center_top_728x90" 
            width="735" 
            height="95"  ></object>
</xsl:if>  

For some reason the closing tag </object> doesn't get rendered in the HTML and causes error. Is there anyway to resolve this?


回答1:


There is no difference (except lexical) in XML between:

<object></object>

and

<object/>

These represent exactly the same XML element, and different XSLT processors are free whichever of the two representations above they choose.

If the long form of the element is really needed in HTML, this can be achieved by either:

  • Using <xsl:output method="xhtml"/> . The xhtml method is only available in XSLT 2.0.

  • Using <xsl:output method="html"/> . The result of the XSLT transformation will be an HTML document (not XML).

  • Using a trick, like:

    <object data="/frontpage_blogroll_center_top_728x90" width="735" height="95"  >
      <xsl:value-of select="$vsomeVar"/>
    </object>
    

where $vsomeVar has no value and will not cause anything to be output, but will trick the XSLT processor into thinking something was output and thus outputting the long form of the element.




回答2:


Use html output method.

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <xsl:if test="true()">
            <object data="/frontpage_blogroll_center_top_728x90"
                    width="735"
                    height="95"  ></object>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

Output:

<object height="95" 
        width="735" 
        data="/frontpage_blogroll_center_top_728x90"></object>

Tested with MSXSL, Xalan, Oracle, Saxon, Altova, XQSharp.



来源:https://stackoverflow.com/questions/4964016/using-xslif-doesnt-include-closing-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!