1

With <output method="xml" indent="yes" encoding="UTF-8"/> xsltproc produces XML files indented by two spaces. Is it possible to change this to four spaces? Full XSLT:

<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">
    <output method="xml" indent="yes" encoding="UTF-8"/>
    <strip-space elements="*"/>

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

    <template match="*">
        <copy>
            <apply-templates select="@*"/>
            <apply-templates>
                <sort select="name()"/>
                <sort select="@*[1]"/>
                <sort select="@*[2]"/>
                <sort select="@*[3]"/>
                <sort select="@*[4]"/>
                <sort select="@*[5]"/>
                <sort select="@*[6]"/>
            </apply-templates>
        </copy>
    </template>
</stylesheet>
l0b0
  • 55,365
  • 30
  • 138
  • 223
  • Some XSLT processors allow proprietary extension attributes for `xsl:output`, like Xalan or Saxon I think, where you can set the indentation amount and/or the characters. I don't know whether xsltproc allows it, what does its manpage say? – Martin Honnen Nov 14 '21 at 10:45
  • For Saxon 9 or 10 (unfortunately only the PE or EE editons) you can set e.g. `` (with `xmlns:saxon="http://saxon.sf.net/"` declared) to define the identation amount. But I think the default in Saxon (including HE) is 4 so if that is your wanted indentation amount perhaps using Saxon instead of xsltproc helps. – Martin Honnen Nov 14 '21 at 10:53
  • 1
    On a side note, the order of attributes is not defined in XSLT/XPath so doing `` could make any attribute node the sort key. – Martin Honnen Nov 14 '21 at 13:18
  • @MartinHonnen I think [it](https://gitlab.com/victor-engmark/make-includes/-/blob/48186555a5d585abdee606da9908fdefcaf0aa52/sort.xslt) was simply the best I could do to sort an XML file in as reproducible a manner as possible. If sorting by attribute names and values is at all possible in XSLT 1.1 it would be interesting to know. [This](https://stackoverflow.com/a/5962936/96588), for example, does not work. – l0b0 Nov 14 '21 at 18:50
  • I don't think you can control the indent amount in `xsltproc` - neither through the stylesheet nor through the command itself. But it should be possible to pipe the result to `tidy` (haven't tested this, though). -- P.S. It is certainly possible to sort by an attribute specified by its name. I suggest you post this as a separate question instead of discussing it in comments. – michael.hor257k Nov 14 '21 at 19:02
  • @michael.hor257k To be clear, it's about sorting *elements* by their attribute names and values. I already linked to an answer which admits it doesn't really work. – l0b0 Nov 14 '21 at 19:52
  • Well, I think it can work - but as I said, I'm not going to discuss this in comments. Post a question with a [mcve] showing input and expected output, then we'll see. – michael.hor257k Nov 14 '21 at 20:03
  • You could try adding an answer to the [question](https://stackoverflow.com/q/5961686/96588) I linked to - if I post this it'll just be a dupe. – l0b0 Nov 14 '21 at 20:29
  • That question is different, because there is only one attribute. You seem to have at least 6. – michael.hor257k Nov 14 '21 at 20:39
  • Good point, [done](https://stackoverflow.com/q/69967340/96588). – l0b0 Nov 14 '21 at 21:29

1 Answers1

1

Now that I have tested this, I can state that it works (on macOS 10.13.6):

xsltproc '/path/to/stylesheet.xsl' '/path/to/input.xml' | tidy -i -xml --indent-spaces 4

Note that this reformats the transformation result - so it doesn't matter if the stylesheet indents or not.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51