Add XSL element at runtime?

Posted on 2009-12-31
Last Modified: 2013-11-18
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?

Question by:dcx45
    LVL 60

    Assisted Solution

    by:Geert Bormans
    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

    Accepted Solution

    Got it now.

    var xslNs = '';
    mySort = xsl.createElementNS(xslNs, 'xsl:sort');
    LVL 60

    Expert Comment

    by:Geert Bormans
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
    Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL ( several years ago, it seemed like now was a good time to updat…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now