Hi All,

I have one scenario,

In xslt i have used a input tag of type="button" with id="button1", Now in xslt if a condtion is met,
suppose
<xsl:if test="price &gt; 10">

i need to disable the button1

Can any one please suggest me how to achieve this using xslt.

Thanks in advance.

Regards,
Sireesha

Dani AI

Generated

Brief answer: emit the disabled attribute on the output button when the price test is true. 's suggestion to add a disabled attribute is the right idea; the key is placing that conditional in the template that actually generates the <input> and making the XPath numeric check robust.

Notes and gotchas:

  • Context matters: the XPath in the test is evaluated against the current context node. If the template that writes the button is not positioned on the node that contains price, reference the full path or a parameter/variable (for example, use number(price) or number($price) to force numeric comparison). If price contains currency symbols or commas, strip/normalize them before number().
  • XSLT construction rules: when adding attributes with XSLT, attribute-creation instructions must occur before any literal element content. In XSLT 1.0 this is typically done with xsl:if/xsl:choose and xsl:attribute; in XSLT 2.0 an AVT can be used inside the element.
  • HTML vs XHTML: for HTML output, presence of the attribute is enough; for XHTML use disabled="disabled". Declare xsl:output method="html" if HTML boolean-attribute behavior is desired.

A compact alternative (XSLT 2.0) is to set the attribute inline with an AVT, e.g. disabled='{if (price > 10) then "disabled" else ()}' on the <input> element. If constrained to XSLT 1.0, add the attribute conditionally inside the template that creates the button so repeated buttons follow the same rule.

For debugging, inspect the generated source (view-source) to verify the transformation produced the attribute (browsers sometimes reformat the DOM). asked for more code — the points above cover the typical fixes needed when the simple conditional doesn't behave as expected.

Recommended Answers

All 2 Replies

show more xml xsl code

An example:
<input>
<xsl:choose>
<xsl:when test="I want to disable it">
<xsl:attribute name="disabled">disabled</xsl:attribute>
</xsl:when>
</xsl:choose>
</input>

try that =o)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.