XSLT - 将父属性值移动到子first para

时间:2017-11-07 12:13:15

标签: xslt

输入就像是,

<section counter="yes" level="5">
<title><target id="page92"/></title>
<section counter="yes" level="6">
<title>Standard 12-lead ECG at Rest</title>
<para>The standard ECG is recorded at rest using 12 leads in order to collect as much information as possible:</para>
<listing type="dash">
<litem><para>Standard limb leads according to Einthoven (I, II, III)</para></litem>

输出应该是,

    <section counter="yes" level="5">
    <title><target /></title>
    <section counter="yes" level="6">
    <title>Standard 12-lead ECG at Rest</title>
    <para id="page92">The standard ECG is recorded at rest using 12 leads in order to collect as much information as possible:</para>
    <listing type="dash">
    <litem><para>Standard limb leads according to Einthoven (I, II, III)</para></litem>

我们写了xslt,如下所示,

<xsl:template match="para[1][parent::section[parent::section[not(normalize-space(title))]]]">
    <xsl:choose>
        <xsl:when test="position() = 1">
            <para>
                <xsl:attribute name="id" select="ancestor::section[not(normalize-space(title))]/title/target/@id"/>
                <xsl:apply-templates select="@*"/>
                <xsl:apply-templates/>
            </para>
        </xsl:when>
        <xsl:otherwise>
            <para>
                <xsl:apply-templates select="@*|node()"/>
            </para>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

使用上面的xslt时,我们无法满足预期的输出。

    <section counter="yes" level="5">
    <title><target /></title>
    <section counter="yes" level="6">
    <title>Standard 12-lead ECG at Rest</title>
    <para id="page92">The standard ECG is recorded at rest using 12 leads in order to collect as much information as possible:</para>
    <listing type="dash">
    <litem><para id="page92">Standard limb leads according to Einthoven (I, II, III)</para></litem>

“页面ID”值重复我们不需要的以下段落。我们只需要在第1段保留页面ID。

请你指导我们。

1 个答案:

答案 0 :(得分:0)

至于获得你想要的结果,它应该像

一样简单
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

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

    <xsl:template match="section[not(normalize-space(title))]/section/para[1]">
        <xsl:copy>
            <xsl:attribute name="id" select="ancestor::section[not(normalize-space(title))]/title/target/@id"/>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="section/title[not(normalize-space())]/target/@id"/>

</xsl:transform>