0

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>
41 72 6c
  • 1,600
  • 5
  • 19
  • 30
  • You can also check it in .NET XSLT Fiddle: https://xsltfiddle.liberty-development.net/bdxtqK/1 – 41 72 6c Oct 18 '18 at 11:21
  • There are various programms that can do that. E.g.: Oxygen, Notepad if you have the XML Tools addon or this website: https://www.freeformatter.com/xml-formatter.html. Im pretty sure that the formatting comes from the default template to apply text. And the text in that case are the linebreaks or indents of the input xml. I also think so you can specify that by the element. That would require a bit research though. – Christian Mosz Oct 18 '18 at 12:28
  • I was also aware of that, but I had the desired structure with different structures. After my subsequent adjustments, however, everything has shifted. But the solution is explained below by Martin. `` fixes the problem and removes whitespace nodes in the content of those elements contained in its token list. – 41 72 6c Oct 18 '18 at 12:51

1 Answers1

3

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.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Hey Martin, yeah that's it. Implemented it in my stylesheets and it worked. Unbelievable how a simple line changes everything. Thank you! – 41 72 6c Oct 18 '18 at 12:46