如何从XSLT中的previous-siblings中获取属性

时间:2016-10-14 20:01:32

标签: xml xslt

我正在尝试将带有嵌入式richtext格式的可怕XML转换为HTML。我找到了一种基于先前兄弟节点中的属性来识别项目符号列表的开头的方法,但我无法弄清楚如何结束项目符号列表。问题是每个文本块的属性是在前一个兄弟节点或系列的第一个段落中定义的。

我有以下XML:

<?xml version="1.0" encoding="UTF-8"?>
<document>
    <item>
        <richtext>
            <pardef/>
            <par def='20'><run>This is the first </run><run>paragraph of the preamble.</run></par>
            <par><run>This is the second paragraph of the </run><run>preamble.</run></par>
            <pardef list='bullet'/>
            <par def='21'><run>This is the </run><run>first bullet.</run></par>
            <par><run>This is the second </run><run>bullet.</run></par>
            <par def='20'><run>This is the first </run><run>paragraph of the conclusion.</run></par>
            <par><run>This is the second paragraph of the </run><run>conclusion.</run></par>
        </richtext>
    </item>
</document>

我想要以下输出:

<p>This is the first paragraph of the preamble.</p>
<p>This is the second paragraph of the preamble.</p>
<ul>
    <li>This is the first bullet.</li>
    <li>This is the second bullet.</li>
</ul>
<p>This is the first paragraph of the conclusion.</p>
<p>This is the second paragraph of the conclusion.</p>

我有以下XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:key name="key-for-par" match="document/item/richtext/par" use="generate-id(preceding-sibling::pardef[1])"/>
    <xsl:output indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates select="document/item/richtext/pardef" />
    </xsl:template>

    <xsl:template match="pardef[@list = 'bullet']">
        <ul>
            <xsl:for-each select="key('key-for-par', generate-id(.))">
                <li>
                    <xsl:value-of select="run" separator=""/>
                </li>
            </xsl:for-each>
        </ul>
    </xsl:template>

    <xsl:template match="pardef[not(@list)]">
            <xsl:for-each select="key('key-for-par', generate-id(.))">
                <p>
                    <xsl:value-of select="run" separator=""/>
                </p>
            </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

根据评论,假设def = 21是子弹列表的开头,那么我们可以忽略&#39; pardef&#39;并使用&#39; par&#39;在选择constract中,我们可以确定何时写入ul,li,p及其相应的结束标记。我在样本上尝试了这个,甚至在它的一些变体上也可以正常工作:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates select="document/item/richtext/par" />
    </xsl:template>

    <xsl:template match="par">
    <xsl:if test="@def and preceding-sibling::par[@def][1][@def='21']"><xsl:text disable-output-escaping="yes">&lt;/ul&gt;</xsl:text></xsl:if>

    <xsl:choose>

        <xsl:when test="@def=21 or (not(@def) and preceding-sibling::par[@def][1][@def='21'])">
            <xsl:if test="@def=21"><xsl:text disable-output-escaping="yes">&lt;ul&gt;</xsl:text></xsl:if>
            <li>
                <xsl:for-each select="run">
                    <xsl:value-of select="text()" separator=""/>
                </xsl:for-each>
            </li>   
        </xsl:when>

        <xsl:when test="not(@def=21)">
            <p>
                <xsl:for-each select="run">
                    <xsl:value-of select="text()" separator=""/>
                </xsl:for-each>
            </p>    
        </xsl:when>     

    </xsl:choose>   

    </xsl:template>
</xsl:stylesheet>