繁体   English   中英

XSLT-将模板应用于子节点和text()节点

[英]XSLT - apply-templates to child node and text() node

我有一个这样的xml文件,

<content>
    <ol>
        <li> List<span style="color: rgb(255,0,255);">The scopeeeee of</span>this project is
            <ol>
                <li>nested list1</li>
                <li>nested <span style="color: rgb(255,0,255);">The scope of this project is</span> list1</li>
                <li>nested list1</li>
                <li>nested list1</li>
                <li>nested list1</li>
            </ol>
        </li>
        <li>
            <p>List<span style="color: rgb(255,0,255);">The scope of this project is to:</span></p>
        </li>
        <li>
            <p>List<span style="color: rgb(255,0,255);">The scope of this project is to:</span></p>
        </li>
    </ol>
</content>

我需要使用XSLT将以上xml转换为以下xml

预期的输出,

<content>
    <orderedList>
        <liItem>
            <para>List<c type="color: rgb(255,0,255);">The scopeeeee of this project is to:</c>jkhsdlkjfh</para>
        </liItem>
        <orderedList>
            <liItem>
                <para>nested list1</para>
            </liItem>
            <liItem>
                <para>nested <c type="color: rgb(255,0,255);">The scope of this project is</c>list1</para>
            </liItem>
            <liItem>
                <para>nested list1</para>
            </liItem>
            <liItem>
                <para>nested list1</para>
            </liItem>
            <liItem>
                <para>nested list1</para>
            </liItem>
        </orderedList>

        <liItem>
            <para> List<c type="color: rgb(255,0,255);">The scope of this project is to:</c></para>
        </liItem>
        <liItem>
            <para> List<c type="color: rgb(255,0,255);">The scope of this project is to:</c></para>
        </liItem>
    </orderedList>
</content>

如您所见,li项目中的文本内容和span节点必须在输出中用<para>节点覆盖。

我写了以下xsl来获取此输出,

<xsl:template match="ol">
        <orderedList>
            <xsl:apply-templates/>
        </orderedList>
    </xsl:template>

    <xsl:template match="li[not(descendant::ol)]" >
        <liItem>
            <para>
                <xsl:apply-templates />
            </para>
        </liItem>
    </xsl:template>

    <xsl:template match="li[descendant::ol]" >
        <liItem>
            <para>
                <xsl:apply-templates select="node()[parent::li][following-sibling::ol]"/>
            </para>
        </liItem>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="span">
        <c type="{@style}">
            <xsl:apply-templates/>
        </c>
    </xsl:template>

    <xsl:template match="li/p">
        <xsl:apply-templates />
    </xsl:template> 

的上面的xsl是唯一的问题是当它具有<ol>内的另一个<ol>项。 我正在努力寻找一种将<para>节点添加到放置在<li><ol>节点之间的文本内容中的方法。

当前输出如下,

<content>
    <orderedList>
        <liItem>
            <para> List<c type="color: rgb(255,0,255);">The scopeeeee of</c>this project is</para>
        </liItem> List<c type="color: rgb(255,0,255);">The scopeeeee of</c>this project is
        <orderedList>
            <liItem><para>nested list1</para></liItem>
            <liItem><para>nested <c type="color: rgb(255,0,255);">The scope of this project is</c>list1</para></liItem>
            <liItem><para>nested list1</para></liItem>
            <liItem><para>nested list1</para></liItem>
            <liItem><para>nested list1</para></liItem>
        </orderedList>
        <liItem><para> List<c type="color: rgb(255,0,255);">The scope of this project is to:</c></para></liItem>
        <liItem><para> List<c type="color: rgb(255,0,255);">The scope of this project is to:</c></para></liItem>
    </orderedList>
</content>

谁能建议我一种方法,如何修改我的代码以获得预期的输出。

如果ol元素总是要放在li元素中的文本之后,则可以将模板更改为此

<xsl:template match="li[descendant::ol]" >
    <liItem>
        <para>
            <xsl:apply-templates select="node()[following-sibling::ol]"/>
        </para>
    </liItem>
    <xsl:apply-templates select="ol"/>
</xsl:template>

也许是这样,如果您实际上想要在liItem使用ol元素,但在para中则不需要

<xsl:template match="li[descendant::ol]" >
    <liItem>
        <para>
            <xsl:apply-templates select="node()[following-sibling::ol]"/>
        </para>
        <xsl:apply-templates select="ol"/>
    </liItem>
</xsl:template>

无论哪种情况,您都可以将匹配li的两个模板合并为一个模板。 像这样:

<xsl:template match="li" >
    <liItem>
        <para>
            <xsl:apply-templates select="node() except ol"/>
        </para>
        <xsl:apply-templates select="ol"/>
    </liItem>
</xsl:template>

另外,如果您想在一个列表项中处理多个ol元素,或者如果在ol元素之后也有文本,则可以使用xsl:for-each-group

<xsl:template match="li" >
    <liItem>
        <xsl:for-each-group select="node()" group-adjacent="boolean(self::ol)">
            <xsl:choose>
                <xsl:when test="current-grouping-key() or not(normalize-space())">
                    <xsl:apply-templates select="current-group() "/>
                </xsl:when>
                <xsl:otherwise>
                    <para>
                        <xsl:apply-templates select="current-group() "/>
                    </para>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each-group>
    </liItem>
</xsl:template>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM