[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

Add XSL element at runtime?

I'm using XSLT to do a runtime transformation of underlying XML file. I selectively need to add sort tag to the XSL.

I've confirmed that I'm inserting in the right place however


does not seem to create the right kind of element (and the sort doesn't work). When I do a debug output the local-name() for all other elements does not have the xsl: prefix, but the sort one does - I believe this has something to do with namespaces etc. but is beyond my current knowledge. Googling isn't helping much.

Any pointers?

  • 2
2 Solutions
Geert BormansCommented:
the problem likely is that whilst adding the element, you don't specify that the prefix xsl: is for the xsl namespace.
Why would you add a sort element dynamically. I highly recommend that you don't do it that way.
What you want to do is not at all robust and safe. There always is a risk that the slightest change to the stylesheet will break this
Given you want to sort based on the content of a child element
I would statically add a sort in that location, like this
<xsl:sort  select="*[name() = $sortelem]"/>
for that you then have a global parameter
    <xsl:param name="sortelem"/>
doing your transform you can pass in a parameter value for $sortelem
This way you dynamically change the XSLT without changing it
This is how I do things like this (mainly when I have tables and I want to change the sorting based on a selected column for example)
You can mix multiple sorts and elements or attributes, all depends on what exactly you need.
but please, don't go adding xsl:sort elements dynamically
dcx45Author Commented:
Got it now.

var xslNs = 'http://www.w3.org/1999/XSL/Transform';
mySort = xsl.createElementNS(xslNs, 'xsl:sort');
Geert BormansCommented:
yes, that would be the way to add the namespace,
read my bit above, I don't recommend that you do it that way, still

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now