为什么我的XSL输出兄弟姐妹作为空元素的子元素?

时间:2018-04-27 18:37:26

标签: html xml xslt

给出以下XML:

    <text>
        <h6>Title</h6>
        <p>Text is right here</p>
        <h6>Title</h6>
        <p>Text is right here</p>
        <pagebreak orientation="portrait@1col" />
        <h2>This is an H2 Title</h2>
        <figure-gallery>
            <figure>
                <figure-headline>Headline</figure-headline>
                <figure-asset>Image tag to display image</figure-asset>
                <figure-caption>Caption of the image</figure-caption>
            </figure>
            <figure>
                <figure-headline>Headline</figure-headline>
                <figure-asset>Image tag to display image</figure-asset>
                <figure-caption>Caption of the image</figure-caption>
            </figure>
        </figure-gallery>
        <h1>This is an H1 title</h1>
    </text>

以下XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xalan" exclude-result-prefixes="xalan">


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

<xsl:template match="pagebreak[1]">
    <div id="empty-html-container" />
</xsl:template>


<xsl:template match="h6">
    <div class="Title">
        <xsl:apply-templates select="@*|node()"/>
    </div>
</xsl:template>

<xsl:template match="p">
    <div class="Text">
        <xsl:apply-templates select="@*|node()"/>
    </div>
</xsl:template>

<xsl:template match="figure-asset">
    <xsl:apply-templates select="@*|node()"/>
</xsl:template>

<xsl:template match="figure">
    <div>
        <header>
            <xsl:copy-of select="figure-headline/p/node()"/>
        </header>
        <section>
            <xsl:apply-templates select="figure-asset"/>
        </section>
        <footer>
            <xsl:value-of select="figure-caption"/>
        </footer>
    </div>
</xsl:template>

<xsl:template match="figure-gallery">
    <div class="figure-gallery">
        <xsl:apply-templates/>
    </div>
</xsl:template>

为什么输出会像这样出现(分页后的兄弟元素是分页模板输出的子代)...

<section>
    <div class="Title">Title</div>
    <div class="BulletText">Text is right here</div>
    <div class="BulletTitle">Title</div>
    <div class="BulletText">Text is right here</div>
    <div class="BulletTitle">Title</div>
    <div class="BulletText">Text is right here</div>
    <div id="empty-html-container">
        <h2>This is an H2 Title</h2>
        <div class="figure-gallery">
            <div>
                <header>Headline</header>
                <section>Image tag to display image</section>
                <footer>Caption of the image</footer>
            </div>
            <div>
                <header>Headline</header>
                <section>Image tag to display image</section>
                <footer>Caption of the image</footer>
            </div>
        </div>
        <h1>This is an H1 title</h1>
    </div>
</section>

...而不是像这样出现(分页后的兄弟元素在分页后仍然是sibiling元素):

<section>
    <div class="Title">Title</div>
    <div class="BulletText">Text is right here</div>
    <div class="BulletTitle">Title</div>
    <div class="BulletText">Text is right here</div>
    <div class="BulletTitle">Title</div>
    <div class="BulletText">Text is right here</div>
    <div id="empty-html-container"></div>
    <h2>This is an H2 Title</h2>
    <div class="figure-gallery">
        <div>
            <header>Headline</header>
            <section>Image tag to display image</section>
            <footer>Caption of the image</footer>
        </div>
        <div>
            <header>Headline</header>
            <section>Image tag to display image</section>
            <footer>Caption of the image</footer>
        </div>
    </div>
    <h1>This is an H1 title</h1>
</section>

空元素是允许JavaScript在浏览器加载页面后做一些工作。

一个奇怪的事情,当我把文本放在输出中的“div”中时,它会按预期工作,但如果我把它留空,那么兄弟姐妹就会变成孩子。

现在,我有一个“br”标签,因为它没有出现,它解决了我的问题,但它增加了一个我不想要的换行符,因为它与我的设计混淆。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

添加     <xsl:output method="html" indent="yes" encoding="utf-8"/> 在样式表声明之后。

产生:     <div id="empty-html-container"></div>

发现于:XML empty tag being converted to open and closing tag pair