Max Li
Max Li

Reputation: 591

In XSLT how to insert a string into HTML tag

I want to add 'disable' into a HTML tag as:

<xsl:template match="item">
  <td>
  <input ng-model="{$ng-model}" class="form-control" disabled style="width: 10em"/>
  </td>
</xsl:tempalte>

However, saxon-HE10 complains

  SXXP0003  Error reported by XML parser: Attribute name "disabled" associated with an
  element type "input" must be followed by the ' = ' character.

So I managed to do it as:

<xsl:template match="item">
  <td colspan="@colspan">
  <xsl:text disable-output-escaping="yes">&lt;input ng-model="{$ng-model}" class="form-control" disabled style="width: 10em" &gt;</xsl:text>
  </td>
</xsl:tempalte>

Which works just fine until I try to capture the output of the template rule into a variable as:

<xsl:variable name="output>
  <xsl:apply-templates select="item"/>
</xsl:variable>

<xsl:copy-of select="$output"/>
<xsl:if test="$output = ''">
<td></td>
</xsl:if>

Here, the xsl:copy-of copy the &it; as it is to the output, which is not desired. I need to use a variable to capture the output of the template rule is because the rule may need match nothing and yield empty string. In that case, I still need to add <td></td> into the output text. Therefore the dilemma here is that in the template rule output there are both < and &it;. If I play with "disable-output-escaping", one of them is not desired. Any solution to my problem?

Upvotes: 0

Views: 126

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167516

In HTML syntax attributes can have no value but in XML/XSLT syntax they always carry one so use disabled="disabled" and as long as these are HTML attributes known to belong to HTML elements the processor, when using xsl:output method html, should serialize them appropriately.

Upvotes: 1

Related Questions