XSLT Sorting this XML...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philipp Lenssen

    #1

    XSLT Sorting this XML...

    I have this XML file which I would like to XSLT-sort based on the
    string contained within the item-children elements using basic MSXML
    (no recent version of IIS, so it might be an outdated MSXML -- pretty
    sure not MSXML4, though tips on how to do it with that are appreciated
    too):

    ----------- Translation Dictionary (German) ----
    <?xml version="1.0" encoding="iso-8859-1"?>
    <items>
    <item id="4">
    <Deutsch>Regist rieren</Deutsch>
    <Englisch-GB>Register</Englisch-GB>
    <Englisch-US>Register</Englisch-US>
    <Französisch>S' enregistrer</Französisch>
    <Italienisch>Re gistra</Italienisch>
    <Spanisch>Dar se de alta</Spanisch>
    <Portugiesisch> </Portugiesisch>
    <Niederländisch >Registreren</Niederländisch>
    <Kommentar></Kommentar>
    </item>
    <item id="5">
    <Deutsch>Logi n</Deutsch>
    <Englisch-GB>Login</Englisch-GB>
    <Englisch-US>Login</Englisch-US>
    <Französisch>Lo gin</Französisch>
    <Italienisch>Lo gin</Italienisch>
    <Spanisch>Inici o de sesión</Spanisch>
    <Portugiesisch> </Portugiesisch>
    <Niederländisch >Login</Niederländisch>
    <Kommentar></Kommentar>
    </item>
    <!-- ... and many more item-elements -->
    </items>
    ------------------------------------------------

    Thanks for any tips!


    (P.S.: Am always disappointed by lack of XPath sorting features. It's a
    hassle compared to e.g. SQL. Last time I had to implement an object
    with an object sorter to sort some integers in XML. I also tried
    XSLT-sorting from examples found online but often ran into troubles, I
    believe it also has to do with limited capabilities of basic-MSXML as
    installed with older versions of IIS.)
  • Martin Honnen

    #2
    Re: XSLT Sorting this XML...



    Philipp Lenssen wrote:
    [color=blue]
    > I have this XML file which I would like to XSLT-sort based on the
    > string contained within the item-children elements using basic MSXML
    > (no recent version of IIS, so it might be an outdated MSXML -- pretty
    > sure not MSXML4, though tips on how to do it with that are appreciated
    > too):
    >
    > ----------- Translation Dictionary (German) ----
    > <?xml version="1.0" encoding="iso-8859-1"?>
    > <items>
    > <item id="4">
    > <Deutsch>Regist rieren</Deutsch>
    > <Englisch-GB>Register</Englisch-GB>
    > <Englisch-US>Register</Englisch-US>
    > <Französisch>S' enregistrer</Französisch>
    > <Italienisch>Re gistra</Italienisch>
    > <Spanisch>Dar se de alta</Spanisch>
    > <Portugiesisch> </Portugiesisch>
    > <Niederländisch >Registreren</Niederländisch>
    > <Kommentar></Kommentar>
    > </item>
    > <item id="5">
    > <Deutsch>Logi n</Deutsch>
    > <Englisch-GB>Login</Englisch-GB>
    > <Englisch-US>Login</Englisch-US>
    > <Französisch>Lo gin</Französisch>
    > <Italienisch>Lo gin</Italienisch>
    > <Spanisch>Inici o de sesión</Spanisch>
    > <Portugiesisch> </Portugiesisch>
    > <Niederländisch >Login</Niederländisch>
    > <Kommentar></Kommentar>
    > </item>
    > <!-- ... and many more item-elements -->
    > </items>[/color]


    Assuming you want to copy all nodes while ordering the children of
    <item> by their content the following is an XSLT 1.0 stylesheet that
    does that:

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

    <xsl:output method="xml" indent="yes" encoding="ISO-8859-1" />

    <xsl:template match="item">
    <xsl:copy>
    <xsl:apply-templates select="@*" />
    <xsl:apply-templates select="*">
    <xsl:sort data-type="text" order="ascendin g" />
    </xsl:apply-templates>
    </xsl:copy>
    </xsl:template>

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

    </xsl:stylesheet>

    The result of the example file transformed with Xalan is

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <items>
    <item id="4">
    <Portugiesisc h/>
    <Kommentar/>
    <Spanisch>Dar se de alta</Spanisch>
    <Englisch-GB>Register</Englisch-GB>
    <Englisch-US>Register</Englisch-US>
    <Italienisch>Re gistra</Italienisch>
    <Niederländisch >Registreren</Niederländisch>
    <Deutsch>Regist rieren</Deutsch>
    <Französisch>S' enregistrer</Französisch>
    </item>
    <item id="5">
    <Portugiesisc h/>
    <Kommentar/>
    <Spanisch>Inici o de sesión</Spanisch>
    <Deutsch>Logi n</Deutsch>
    <Englisch-GB>Login</Englisch-GB>
    <Englisch-US>Login</Englisch-US>
    <Französisch>Lo gin</Französisch>
    <Italienisch>Lo gin</Italienisch>
    <Niederländisch >Login</Niederländisch>
    </item>
    <!-- ... and many more item-elements -->
    </items>


    As for MSXML, as far as I know MSXML 3.0 is the first to support XSLT
    1.0 (which is indentified by the namespace URI
    http://www.w3.org/1999/XSL/Transform). Earlier versions only support
    some working draft (WD) with a different namespace and different
    elements and semantics. I stronly suggest to get your ISP to install at
    least MSXML 3.0 if you want to do XSLT 1.0 transformations .
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Philipp Lenssen

      #3
      Re: XSLT Sorting this XML...

      Martin Honnen wrote:
      [color=blue]
      > Philipp Lenssen wrote:[/color]
      [color=blue][color=green]
      > > <?xml version="1.0" encoding="iso-8859-1"?>
      > > <items>
      > > <item id="4">
      > > <Deutsch>Regist rieren</Deutsch>
      > > <Englisch-GB>Register</Englisch-GB>
      > > <Englisch-US>Register</Englisch-US>
      > > <Französisch>S' enregistrer</Französisch>
      > > <Italienisch>Re gistra</Italienisch>
      > > <Spanisch>Dar se de alta</Spanisch>
      > > <Portugiesisch> </Portugiesisch>
      > > <Niederländisch >Registreren</Niederländisch>
      > > <Kommentar></Kommentar>
      > > </item>
      > > <item id="5">
      > > <Deutsch>Logi n</Deutsch>
      > > <Englisch-GB>Login</Englisch-GB>
      > > <Englisch-US>Login</Englisch-US>
      > > <Französisch>Lo gin</Französisch>
      > > <Italienisch>Lo gin</Italienisch>
      > > <Spanisch>Inici o de sesión</Spanisch>
      > > <Portugiesisch> </Portugiesisch>
      > > <Niederländisch >Login</Niederländisch>
      > > <Kommentar></Kommentar>
      > > </item>
      > > <!-- ... and many more item-elements -->
      > > </items>[/color]
      >
      >
      > Assuming you want to copy all nodes while ordering the children of
      > <item> by their content the following is an XSLT 1.0 stylesheet that
      > does that:
      >[/color]

      I'm sorry, I should have made this clear: I want to sort by a certain
      language (the children of "item"-element are language-names in German).
      For example, I pass the parameter "Deutsch", and then it should sort
      alphabetically by the "Deutsch"-element's text-content. Same for e.g.
      "Italienisc h". I will keep your current solution for another day.
      Thanks for your help so far!

      Comment

      • Mats Kindahl

        #4
        Re: XSLT Sorting this XML...

        "Philipp Lenssen" <info@outer-court.com> writes:
        [color=blue]
        > Martin Honnen wrote:
        >[color=green]
        > > Philipp Lenssen wrote:[/color]
        >[color=green][color=darkred]
        > > > <?xml version="1.0" encoding="iso-8859-1"?>
        > > > <items>
        > > > <item id="4">
        > > > <Deutsch>Regist rieren</Deutsch>
        > > > <Englisch-GB>Register</Englisch-GB>
        > > > <Englisch-US>Register</Englisch-US>
        > > > <Französisch>S' enregistrer</Französisch>
        > > > <Italienisch>Re gistra</Italienisch>
        > > > <Spanisch>Dar se de alta</Spanisch>
        > > > <Portugiesisch> </Portugiesisch>
        > > > <Niederländisch >Registreren</Niederländisch>
        > > > <Kommentar></Kommentar>
        > > > </item>
        > > > <item id="5">
        > > > <Deutsch>Logi n</Deutsch>
        > > > <Englisch-GB>Login</Englisch-GB>
        > > > <Englisch-US>Login</Englisch-US>
        > > > <Französisch>Lo gin</Französisch>
        > > > <Italienisch>Lo gin</Italienisch>
        > > > <Spanisch>Inici o de sesión</Spanisch>
        > > > <Portugiesisch> </Portugiesisch>
        > > > <Niederländisch >Login</Niederländisch>
        > > > <Kommentar></Kommentar>
        > > > </item>
        > > > <!-- ... and many more item-elements -->
        > > > </items>[/color][/color][/color]

        [snip]
        [color=blue]
        > I'm sorry, I should have made this clear: I want to sort by a certain
        > language (the children of "item"-element are language-names in German).
        > For example, I pass the parameter "Deutsch", and then it should sort
        > alphabetically by the "Deutsch"-element's text-content. Same for e.g.
        > "Italienisc h". I will keep your current solution for another day.
        > Thanks for your help so far![/color]

        I'm not familiar with MSXML (I'm using Xalan), but something like this
        should work...

        <?xml version="1.0"?>
        <xsl:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" indent="yes" encoding="ISO-8859-1"/>

        <xsl:template match="items">
        <xsl:apply-templates select="item">
        <xsl:sort select="item/Deutsch"/>
        </xsl:apply-templates>
        </xsl:template>

        <!-- insert all other templates to process the items -->

        </xsl:stylesheet>

        Best wishes,
        Mats Kindahl
        --
        IAR Systems in Uppsala, Sweden.

        Any opinions expressed are my own and not those of my company.

        Spam prevention: contact me at mNaOtSkPiAnM@ac m.org or
        mNaOtSs.kPindAa hMl@iar.se, removing the *NO SPAM* from the address.

        Comment

        Working...