XSL:Sort with dynamic xpath not working

Hi,
Again a same problem with xpath not working with xsl:sort on a dynamic expression. extract of the code is posted here.
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ns1="http://www.example.org"
                exclude-result-prefixes="xsl ns1 xsd xref xp20 bpws ora ehdr orcl ids hwf">
               
                <xsl:param name="SortCondition1" select="ns1:last" />
                 <xsl:variable name="SC">
                  <xsl:value-of select="$SortCondition1" />
                </xsl:variable>

  <xsl:template match="/">
    <ns1:employees>
      <xsl:for-each select="//ns1:employees/ns1:employee">
      <xsl:sort select="*[name()=$SC]" order="descending"/>
        <ns1:employee>
          <ns1:first>
            <xsl:value-of select="ns1:first"/>
          </ns1:first>
          <ns1:last>
            <xsl:value-of select="ns1:last"/>
          </ns1:last>
          <ns1:salary>
            <xsl:value-of select="ns1:salary"/>
          </ns1:salary>
        </ns1:employee>
      </xsl:for-each>
    </ns1:employees>
  </xsl:template>
</xsl:stylesheet>

Input XML
-----------

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header/>
    <soap:Body xmlns:ns1="http://www.example.org">
        <ns1:employees>
            <ns1:employee>
                <ns1:first>asd</ns1:first>
                <ns1:last>asd</ns1:last>
                <ns1:salary>123.45</ns1:salary>
            </ns1:employee>
            <ns1:employee>
                <ns1:first>zxc</ns1:first>
                <ns1:last>zxc</ns1:last>
                <ns1:salary>1723.45</ns1:salary>
            </ns1:employee>
        </ns1:employees>
    </soap:Body>
</soap:Envelope>
LVL 4
gcmachelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert BormansInformation ArchitectCommented:
<xsl:param name="SortCondition1" select="ns1:last" />

the problem is that you are not using ns1:last as a literal string,
but as the node with that name
I suppose you want to use the string value,
then you should add single quotes around it

<xsl:param name="SortCondition1" select="'ns1:last'" />

cheers

Geert
0
gcmachelAuthor Commented:
Hi Gertone,

The value for the SortCondition1 param will be populated dynamically.

means another application passes the value for the SortCondition1...it will not have default value.

Regards - G.
0
Geert BormansInformation ArchitectCommented:
Hi G.,

Then you should have a careful look at the process that passes in the parameter
I have tested your XSLT and it works fine, including the dynamic sorting

you could by the way simplify your tylesheet

    <xsl:template match="/">
        <ns1:employees>
            <xsl:for-each select="//ns1:employees/ns1:employee">
                <xsl:sort select="*[name()=$SC]" order="descending"/>
                    <xsl:copy-of select="."/>
            </xsl:for-each>
        </ns1:employees>
    </xsl:template>

cheers

Geert
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

gcmachelAuthor Commented:
Hi Gertone,
This is what i tested and the results are posted. Let me know what is the problem and why its not sorting.
xsl
------
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ns1="http://www.example.org">
               
                <xsl:param name="SortCondition1" select="ns1:last" />
                 <xsl:variable name="SC">
                  <xsl:value-of select="$SortCondition1" />
                </xsl:variable>

<xsl:template match="/">
        <ns1:employees>
            <xsl:for-each select="//ns1:employees/ns1:employee">
                <xsl:sort select="*[name()=$SC]" order="descending"/>
                    <xsl:copy-of select="."/>
            </xsl:for-each>
        </ns1:employees>
    </xsl:template>


</xsl:stylesheet>

xml
--------
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header/>
    <soap:Body xmlns:ns1="http://www.example.org">
        <ns1:employees>
            <ns1:employee>
                <ns1:first>asd</ns1:first>
                <ns1:last>asd</ns1:last>
                <ns1:salary>123.45</ns1:salary>
            </ns1:employee>
            <ns1:employee>
                <ns1:first>zxc</ns1:first>
                <ns1:last>zxc</ns1:last>
                <ns1:salary>1723.45</ns1:salary>
            </ns1:employee>
        </ns1:employees>
    </soap:Body>
</soap:Envelope>

Output
--------
<?xml version="1.0" encoding="UTF-8"?>
<ns1:employees xmlns:ns1="http://www.example.org">
      <ns1:employee xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
            <ns1:first>asd</ns1:first>
            <ns1:last>asd</ns1:last>
            <ns1:salary>123.45</ns1:salary>
      </ns1:employee>
      <ns1:employee xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
            <ns1:first>zxc</ns1:first>
            <ns1:last>zxc</ns1:last>
            <ns1:salary>1723.45</ns1:salary>
      </ns1:employee>
</ns1:employees>

See the sort is not happening in output and it should get sorted by last name. Not sure why is it happening like this.
Regards,G.
0
Geert BormansInformation ArchitectCommented:
The code as such is OK.
The problem is in your default value for the parameter
It is accessing a node, it should be a string, so you need to add an extra pair of single quotes around the  ns1:last
like this
    <xsl:param name="SortCondition1" select="'ns1:last'" />

If it doesn't work, when you pass in the parameter,
you need to verify whether the pass-in was done correctly
0
gcmachelAuthor Commented:
Gertone,
You are correct, This parameter i am passing just for us to test it. But actually the value is passed from a BPEL program which will pass just ns1:last alone. so what would be your suggestion in this place. The actual lines in my XSL would be some thing like this <xsl:param name="SortCondition1" />

What should i do here? should i manually add two quotes around? I tried it that too doesnt work. any suggestions?
Regards,G.
0
Geert BormansInformation ArchitectCommented:
if it is passed from a BPEL program,
the string passed in will be interpreted as a string, not an XPath expression,
so you should be safe in that regard.
I assume that the value is not really caught.
Can you output the param with a value-of somewhere in your XSLT, maybe between [[ ]], so it is obvious
so you can see what you are actually passing
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.