簡體   English   中英

通過prettyprint正確表示代碼

[英]Code for correct representation via prettyprint

我的代碼表示有問題。 我的XSLT正確處理了此問題,但是輸出很丑陋,與預期不符。 通常, pretty-print功能可以完美運行,但是在我更改之后不起作用,我也不知道該怎么辦。 我認為我在這里使用的<xsl:apply-templates>有問題。 漂亮的印刷后,有沒有辦法得到漂亮的代碼?

注意:我正在使用XSLT版本1,並且我正在Altova XMLSpy中進行編碼。

這是我的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>

這是我的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>

正如我提到的,結果是正確的,只是縮進和表示。 那應該是這樣的:

<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>

但目前看起來像這樣:

<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>

如果您想要一致的縮進並且沒有混合的內容輸入,那么最簡單的方法通常是添加空白剝離和標識,例如

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

(輸出方法html並不是必需的,但是您的輸出格式似乎是HTML,因此,如果您告訴XSLT處理器,則可能會得到更好的結果,因為這樣的話,已知的HTML內聯元素或空元素將根據HTML規則而不是XML進行序列化規則)。

使用建議的方法,您在https://xsltfiddle.liberty-development.net/bdxtqK/2上的示例看起來不錯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM