Is it possible to create an attribute in square brackets in XSLT 1.0

580 Views Asked by At

There is a code snippet for AMP pages carousel with some interactivity https://ampbyexample.com/advanced/image_galleries_with_amp-carousel/

<amp-carousel controls
  width="400"
  height="100">
  <amp-img src="/img/image1.jpg"
    width="100"
    height="75"
    role="button"
    tabindex="0"
    [class]="selectedSlide == 0 ? 'selected' : ''"
    on="tap:AMP.setState({selectedSlide: 0})"></amp-img>
  <amp-img src="/img/image2.jpg"
    width="100"
    height="75"
    role="button"
    tabindex="1"
    [class]="selectedSlide == 1 ? 'selected' : ''"
    on="tap:AMP.setState({selectedSlide: 1})"></amp-img>
  <amp-img src="/img/image3.jpg"
    width="100"
    height="75"
    role="button"
    tabindex="2"
    [class]="selectedSlide == 2 ? 'selected' : ''"
    on="tap:AMP.setState({selectedSlide: 2})"></amp-img>
</amp-carousel>

I'd like to integrate it for my pages using XSLT. The problem is that attribute of element uses attribute with square brackets. Is it possible to create such element with such specific attribute ([class]="selectedSlide == 1 ? 'selected' : ''") in XSLT?

2

There are 2 best solutions below

1
On BEST ANSWER

I haven't come across this not-quite-XML dialect before, but XSLT doesn't support serialization to this format. You could achieve it by some kind of post-processing, for example output the attribute name as _class_="value" and then use some kind of regex processing to change _class_ to [class].

If you're feeling enterprising, Saxon allows you to implement your own serialization methods, so you could subclass the XML serialization method to do a similar substitution of _class_ to [class], or perhaps put the special attributes in a namespace that the serializer recognizes.

1
On

XML itself does not permit attribute names to contain square brackets.

You could output text of the form you describe, but that's highly not recommended. It certainly would not be XML.