-1

I have an xml config file:

 <icecast>
    <mount>
    <mount-name>/yourradio.mp3</mount-name>
    <accesslog>
    <name>/access/yourradio/yourradiomp3.log</name>
    <duration>86400</duration>
    <archive>true</archive>
    <size>20000000</size>
    </accesslog>
    </mount>

    <mount>
    <mount-name>/aire.mp3</mount-name>
    <accesslog>
    <name>/access/yourradio/yourradiomp3.log</name>
    <duration>86400</duration>
    <archive>true</archive>
    <size>20000000</size>
    </accesslog>
    </mount>
 </icecast>

How can I sort this in alphabetic order using the <mount-name> as the sorting element and output it back into xml. I have seen you can do some stuff with xmllint using a xslt but I cant seem to get my head around it and get it to work.

Ideally would love to be able to run this in a bash script to then sort all of the mounts I have in a configuration file.

Thank you for all of your help!

Stuart
  • 51
  • 6
  • 2
    Place XSLT tried so far, and what is the required result?, your XML is not well formed, because there is no ROOT element. – Rudramuni TP Feb 04 '15 at 13:34
  • I will have a look at the sort XML nodes one. Rudramuni thank you for this I only did a snipit of the code the root element is – Stuart Feb 04 '15 at 14:15
  • I have tried to replicate the example they have used but I can't get it to work. When i create the xslt how do i generate the new xml? I have been trying: xmllint sorted.xml unsorted.xml --output output.xml Is this right? – Stuart Feb 04 '15 at 14:21
  • 1
    I don't think you can do an XSLT transform with xmllint. I think you'd have to use xsltproc or xmlstarlet (or my preference: saxon (java jar)). – Daniel Haley Feb 04 '15 at 17:21

1 Answers1

3

Try this:

<xsl:stylesheet version="2.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="icecast">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()">
                <xsl:sort select="mount-name"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
Rudramuni TP
  • 1,268
  • 2
  • 16
  • 26
  • 1
    You've already got the identity transform; why use `xsl:for-each`? Just use `xsl:apply-templates` with an `xsl:sort` inside of it. Like this (sorry for pasting code in a comment): `` – Daniel Haley Feb 04 '15 at 17:11
  • @DanielHaley, You are welcome and once again thanks for smart code. – Rudramuni TP Feb 04 '15 at 17:27
  • I think im getting there thank you for all of your help! Just trying to work out how I can pass the XSLT so I can use it in a bash script. The xsltproc failed with a "xsl:version: only 1.0 features are supported" But if i try via the w3c tool it does come back in the correct format so I have the XSLT setup right now. – Stuart Feb 05 '15 at 10:19
  • I have this all working now thank you for everyones help! – Stuart Feb 05 '15 at 12:21