Xml 创建具有属性的元素

Xml 创建具有属性的元素,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我想通过匹配@pagebreak创建一个元素@pagebreak与多个父项一起出现,如para、table等 输入XML: 问题在于您的模板匹配*[@pagebreak]。与执行xsl:apply模板不同,您应该执行xsl:next匹配,以便继续匹配当前节点,而不是继续匹配其子节点 <xsl:template match="*[@pagebreak]"> <div><pagebreak type="yes"/></div> <

我想通过匹配@pagebreak创建一个元素@pagebreak与多个父项一起出现,如para、table等

输入XML:


问题在于您的模板匹配*[@pagebreak]。与执行xsl:apply模板不同,您应该执行xsl:next匹配,以便继续匹配当前节点,而不是继续匹配其子节点

<xsl:template match="*[@pagebreak]">
    <div><pagebreak type="yes"/></div>
    <xsl:next-match />
</xsl:template>
<?xml version="1.0" encoding="UTF-8"?>
<article>
    <section>
        <p class="p_1">This is first para.</p>
        <div>
            <pagebreak type="yes"/>
        </div>
        <para>This is second para.</para>
        <p class="p_3">This is first para.</p>
        <table id="tab_1">
            <tr>
                <td>first table</td>
            </tr>
        </table>
    </section>
    <section>
        <p class="p_1">This is first para.</p>
        <p class="p_2">This is second para.</p>
        <p class="p_3">This is first para.</p>
        <div>
            <pagebreak type="yes"/>
        </div>
        <table>
            <tr>
                <td>second table</td>
            </tr>
        </table>
    </section>
</article>
<?xml version="1.0" encoding="UTF-8"?>
<article>
    <section>
        <p class="p_1">This is first para.</p>
        <div>
            <pagebreak type="yes"/>
        </div>
        <p class="p_2">This is second para.</p>
        <p class="p_3">This is first para.</p>
        <table id="tab_1">
            <tr>
                <td>first table</td>
            </tr>
        </table>
    </section>
    <section>
        <p class="p_1">This is first para.</p>
        <p class="p_2">This is second para.</p>
        <p class="p_3">This is first para.</p>
        <div>
            <pagebreak type="yes"/>
        </div>
        <table id="tab_2">
            <tr>
                <td>second table</td>
            </tr>
        </table>
    </section>
</article>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">

    <xsl:output indent="yes"/>

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

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

    <xsl:template match="para">
        <p class="{concat('p_', count(preceding-sibling::para)+1)}">
            <xsl:apply-templates select="@* | node()"/>
        </p>
    </xsl:template>

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

    <xsl:template match="table">
        <xsl:variable name="num"><xsl:number/></xsl:variable>
        <table id="{concat('tab_', $num)}">
            <xsl:apply-templates select="@* | node()"/>
        </table>
    </xsl:template>


    <xsl:template match="*[@pagebreak]">
        <div><pagebreak type="yes"/></div>
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>


</xsl:stylesheet>
<xsl:template match="*[@pagebreak]">
    <div><pagebreak type="yes"/></div>
    <xsl:next-match />
</xsl:template>
<xsl:template match="@pagebreak" />