2

I have an XSL transformation where I used to query a string that way:

<xsl:value-of select="/input/as/a[@id=$id]/CaMeL[@id2=$id2]/@interest"/>

Now, it happened that CaMeL (in the XML) shall be renamed to cAmEl. Fine. Simply chaning the case in my transform would immediately work but I would lose backward compatibilty.

Searching at SO, I found this: XSLT Stylesheet: Changing text to upper case

The accepted answer looks promising but I'm currently stuck at how to fill the doc parameter used in the answer.

How could I add a translation step in my existing <xsl:template match="/"> that translates the whole doc to lower case before any other templates are going to be applied?

Community
  • 1
  • 1
eckes
  • 64,417
  • 29
  • 168
  • 201

1 Answers1

3

Maybe this can help. But I didn't test it. Could you provide some sample XML?

<xsl:value-of select="/input/as/a[@id=$id]/*[translate(local-name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='camel' and @id2=$id2]/@interest"/>

EDIT

If you want to change your whole XML file to lower case (without elements and attributes values of course) you can use this following template:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
    <xsl:element name="{translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
                                         'abcdefghijklmnopqrstuvwxyz')}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

Having done the conversion to lower-case, you can try to create multi-pass XSLT traformation - the next pass would be your conversion. However, I'm not sure how to do it using XSLT 1.0.

EDIT 2

OK, so here comes the whole example. I didn't have your examples so I worked on my own ones.

Sample input file:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
<baba>aaa</baba>
<Baba>BBB</Baba>
</Root>

XSLT with multi-pass sorting:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="firstPassResult">
        <xsl:apply-templates select="/" mode="firstPass"/>
    </xsl:variable>
    <xsl:template match="@*|node()" mode="firstPass">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*" mode="firstPass">
        <xsl:element name="{translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
                                         'abcdefghijklmnopqrstuvwxyz')}">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="/">
        <xsl:apply-templates select="$firstPassResult" mode="secondPass"/>
    </xsl:template>
    <xsl:template match="//baba" mode="secondPass">
        <xsl:value-of select="text()"/>
    </xsl:template>
</xsl:stylesheet>

I works under Altova XMLSpy debugger. Output:

<?xml version="1.0" encoding="UTF-8"?>aaaBBB
Lukasz
  • 7,572
  • 4
  • 41
  • 50