Adding attribute to HTML output with XSLT

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drhowarddrfine
    Recognized Expert Expert
    • Sep 2006
    • 7434

    #1

    Adding attribute to HTML output with XSLT

    I have this in an XSLT stylesheet sitting in a browser:
    Code:
    <xsl:variable name="name" select="@name" />
         <xsl:variable name="input-type" select="@input" />
         <xsl:value-of select="title" />
          <xsl:for-each select="item">
              <input type="{$input-type}"
               name="{$name}" value="{@value}" />
               <xsl:value-of select="."/>
          </xsl:for-each>
    This all works fine but I need to have a "checked" attribute conditionally inserted before the closing tag based on xml sent from the server tagged as
    Code:
    <item value="stuff" checked="checked"></item>
    The inserted attribute should be "checked" and not 'checked=""'. I've not worked with my XML stuff for a while and I just can't recall how to do this.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    checked (with no value) is an invalid attribute in XML (therefore XSLT won’t write that in xml mode). either use checked="checke d" (where the value is gracefully ignored) or try the html output option (not sure if it works, though).

    Comment

    • drhowarddrfine
      Recognized Expert Expert
      • Sep 2006
      • 7434

      #3
      The xml comes down as 'checked="check ed". I want the output of the xslt to be just "checked". The output is in the xhtml namespace.

      EDIT: Oh, but that's XML, too. Have to think this through. I may have had it working without thinking about the standalone "checked".

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        checked="true" or checked="" won't work for you?
        Code:
        <input ...>
          <xsl:if test="someCondition">
            <xsl:attribute name="checked">checked</xsl:attribute><!-- you can change value inside element if needed -->
          </xsl:if>
        </input>
        Last edited by jkmyoung; May 10 '10, 06:44 PM. Reason: made value checked.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          checked="true" or checked="" won't work for you?
          XHTML requires it to be checked="checke d". this goes for all HTML standalone attributes in XHTML.

          Comment

          • drhowarddrfine
            Recognized Expert Expert
            • Sep 2006
            • 7434

            #6
            Now I know what I was forgetting. I didn't add the closing tag </input>

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              or just use the self closing tag
              Code:
              <input … />
              via
              Code:
              <xsl:element name="input">
                  <!-- further definitions -->
              </xsl:element>

              Comment

              Working...