xslt 将列表元素分发到节点

xslt distribute list elements to nodes

我想将 xml 转换为以下结构

<R>
    <S>
            <SN>S00</SN>
            <SN>S01</SN>
            <SN>S02</SN>
    </S>
    <L>
        <ID>100</ID>
        <Q>1</Q>
    </L>
    <L>
        <ID>200</ID>
        <Q>2</Q>
    </L>
</R>

为此:

<R>
    <L>
        <ID>100</ID>
        <Q>1</Q>
        <S>
            <SN>S00</SN>
        </S>
    </L>
    <L>
        <ID>200</ID>
        <Q>2</Q>
        <S>
            <SN>S01</SN>
            <SN>S02</SN>
        </S>    
    </L>
</R>

说明:任务是将S元素的元素分配给L元素。相应 L 元素中 Q 的值控制应将 S 中的多少元素移动到 L 元素。 S 元素的顺序无关紧要。

考虑这样一个实际案例:生产批次 (ID) 中有数量 (Q) 的商品,我们想在每个商品上贴上带有序列号 (SN) 的徽章。转换的结果是:"glue the badge with serial number S00 to the one item from batch 100 and the serial numbers S01 and S02 to the two items from batch 200."

有趣的问题。这是您可以查看的一种方式:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="Q">
    <xsl:variable name="start" select="sum(../preceding-sibling::L/Q)" />
    <xsl:variable name="end" select="$start + ." />
    <xsl:copy-of select="."/>
    <S>
        <xsl:copy-of select="../../S/SN[$start &lt; position() and position() &lt;= $end]"/>
    </S>   
</xsl:template>

<xsl:template match="S"/>

</xsl:stylesheet>

这种方法的问题是它不是很有效:每个 L 节点计算其所有前一个兄弟的 Q 值的总和,而不是仅仅将其值添加到累加总和中。

如果您的输入有大量节点,请考虑递归方法 - 例如:

XSLT 1.0

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

<xsl:template match="/R">
    <xsl:copy>
        <xsl:call-template name="gen">
            <xsl:with-param name="batches" select="L"/>
            <xsl:with-param name="labels" select="S/SN"/>
        </xsl:call-template>
    </xsl:copy>
</xsl:template>

<xsl:template name="gen">
    <xsl:param name="batches" select="/.."/>
    <xsl:param name="labels" select="/.."/>
    <xsl:variable name="batch" select="$batches[1]" />
    <xsl:variable name="q" select="$batch/Q" />
    <xsl:if test="$batches">
        <L>
            <xsl:copy-of select="$batch/ID | $q"/>
            <S>
                <xsl:copy-of select="$labels[position() &lt;= $q]"/>
            </S>  
        </L>
        <!-- recursive call -->
        <xsl:call-template name="gen">
            <xsl:with-param name="batches" select="$batches[position() > 1]"/>
            <xsl:with-param name="labels" select="$labels[position() > $q]"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

您可以对 L 元素使用兄弟递归,传递剩余的 S 元素:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

    <xsl:template match="R">
        <xsl:copy>
            <xsl:variable name="sn" select="S/SN"/>
            <xsl:apply-templates select="L[1]">
                <xsl:with-param name="sn" select="$sn"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="L">
        <xsl:param name="sn"/>
        <xsl:copy>
            <xsl:copy-of select="*"/>
            <S>
                <xsl:copy-of select="$sn[position() &lt;= current()/Q]"/>
            </S>
        </xsl:copy>
        <xsl:apply-templates select="following-sibling::L[1]">
            <xsl:with-param name="sn" select="$sn[position() > current()/Q]"/>
        </xsl:apply-templates>
    </xsl:template>

</xsl:stylesheet>