htmlxmlxsltxpathxmlspy

Code for correct representation via prettyprint


I have a problem with the representation of my code. This is handled correctly by my XSLT, but the output is ugly and not as expected. Normally, the pretty-print-Function works perfectly, but not after my changes and I don't know what to do. I think there is a problem with my <xsl:apply-templates> I'm using here. Is there a way to get a beautiful code after pretty printing?

As a note: Im using XSLT Version 1 and I am coding in Altova XMLSpy.

This is my XML:

<University>
     <Students>
         <Info>Example 01</Info>
         <List>
             <ListEntry>
                 <Info>This is my first entry.</Info>
             </ListEntry>
             <ListEntry>
                 <Info>This is my second entry.</Info>
             </ListEntry>
         </List>
    </Students>
    <Students>
         <Info>Example 02</Info>
    </Students>
    <Students>
         <Info>Example 03</Info>
    </Students>
    <AlternativeStudents>
        <Students>
            <Info>Alternative Example 04</Info>
            <Info>Alternative Example 05</Info>
        </Students>
    </AlternativeStudents>
</University>

And this is my XSLT:

<!-- UNIVERSITY -->
<xsl:template match="University">
    <div data-class="greycontainer">
        <p data-role="heading">
            <xsl:text>STUDENTS</xsl:text>
        </p>
        <ul>
            <xsl:apply-templates/>
        </ul>
    </div>
</xsl:template>
<!-- STUDENTS -->
<xsl:template match="Students">
    <xsl:apply-templates/>
    <!-- "or" for AlternativeStudents -->
    <xsl:if test="following-sibling::*[1][self::AlternativeStudents]">
        <li class="parablock bold_">
            <xsl:text>or</xsl:text>
        </li>
    </xsl:if>
</xsl:template>
<!-- ALTERNATIVESTUDENTS -->
<xsl:template match="AlternativeStudents>
    <xsl:apply-templates/>
</xsl:template>
<!-- INFO IN LISTENTRY AND STUDENTS -->
<xsl:template match="ListEntry/Info | Students/Info">
    <xsl:choose>
        <!-- First -->
        <xsl:when test="name(preceding-siblings::*[1])!='Info'">
            <li>
                <xsl:apply-templates/>
            </li>
        </xsl:when>
        <!-- Following -->
        <xsl:otherwise>
            <li class="parablock">
                <xsl:apply-templates/>
            </li>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
<!-- LIST -->
<xsl:template match="List">
    <ul>
        <xsl:apply-templates/>
    </ul>
</xsl:template>
<!-- LISTENTRY -->
<xsl:template match="ListEntry>
    <xsl:apply-templates/>
</xsl:template>

And the result is correct as I mentioned, its just the indentations and presentation. And that is how it SHOULD look like:

<div data-class="greycontainer">
    <p data-role="heading">STUDENTS</p>
    <ul>
        <li>Example 01</li>
        <ul>
            <li>This is the first entry.</li>
            <li>This is the second entry.</li>
        </ul>
        <li>Example 02</li>
        <li>Example 03</li>
        <li class="parablock bold_">or</li>
        <li>AlternativeExample 04</li>
        <li class="parablock">AlternativeExample 05</li>
    </ul>
</div>

But it looks like this at the moment:

<div data-class="greycontainer"><p data-role="heading">STUDENTS</p><ul>

            <li>Example 01</li>
            <ul>

                    <li>This is the first entry.</li>


                    <li>This is the second entry.</li>

            </ul>


            <li>Example 02</li>


            <li>Example 03</li>
        <li class="parablock bold_">or</li>


                <li>AlternativeExample 04</li>
                <li class="parablock">AlternativeExample 05</li>


   </ul></div>

Solution

  • If you want a consistent indentation and don't have mixed content input then the easiest is usually to add both white-space stripping and identation with e.g.

    <xsl:output method="html" indent="yes"/>
    <xsl:strip-space elements="*"/>
    

    (the output method html is not necessary for that but your output format seems to be HTML and therefore you might get better results if you tell the XSLT processor as that way known HTML inline elements or empty elements are serialized according to HTML rules and not XML rules).

    Your example at https://xsltfiddle.liberty-development.net/bdxtqK/2 seems to look fine with the suggested approach.