【问题标题】:xslt distribute list elements to nodesxslt 将列表元素分发到节点
【发布时间】:2016-11-26 03:04:09
【问题描述】:

我想用下面的结构转换一个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) 的徽章。转换结果为:“将序列号为 S00 的徽章粘贴到批次 100 的一件商品上,将序列号 S01 和 S02 的徽章粘贴到批次 200 的两件商品上。”

【问题讨论】:

  • 欢迎来到 SO,@Bernhard!你试过什么?问题出在哪里?您的帖子不包含问题/问题,除了“请为我做”这不是 SO 的用途......所以请编辑问题以显示有问题的代码
  • 你可以使用 XSLT 2.0 吗?
  • 我只能使用 XSLT 1.0

标签: list xslt distribute


【解决方案1】:

您可以在 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>

【讨论】:

    【解决方案2】:

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

    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>
    

    【讨论】:

    • 非常感谢您的帮助!我已经考虑过递归方法,但我还不能让它工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    相关资源
    最近更新 更多