1

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?

3
  • Good question, +1. See my answer for explanation and for three complete solutions. :) Feb 10, 2011 at 23:53
  • I need to keep the test="position() = 3" . I am using it in a blog roll and want to insert it in the middle of one. Feb 11, 2011 at 0:10
  • Are you satisfied with the answers or do you still have a problem? Feb 12, 2011 at 0:14

2 Answers 2

4

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.

11
  • Where can I use it with <xsl:if test="position() = 3"> I want to use it with a blogroll so that it inserts after position 3 Feb 11, 2011 at 0:01
  • The $vsomeVar trick doesn't always work. Also, bear in mind it doesn't matter what the XSLT does if the output is an object representing an XML DOM rather than textual XML source; it's whatever serializes the XML DOM to a string that makes the difference. NB: This is a general observation, and might not apply to this question.
    – Flynn1179
    Feb 11, 2011 at 0:12
  • 1
    @Flynn1179: In my practice this has worked so far. I have seen Michael Kay also recommend this trick. Feb 11, 2011 at 2:45
  • @BlindingDawn: Just place the code in the body of your <xsl:if> Feb 11, 2011 at 2:48
  • I don't know where the $vsomeVar idea comes from. It certainly won't have any effect in Saxon. I think the answer to this question is: use the right output method, and make sure the serialization is done by the XSLT processor, not by some subsequent process. Feb 11, 2011 at 9:32
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.

1
  • +1. Your answer is also correct and you have spent time and effort writing it. Feb 11, 2011 at 2:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.