我想用以下结构转换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)的徽章粘贴到每个单独的项目上。转换的结果是:&#34;将带有序列号S00的徽章粘贴到批次100中的一个项目,将序列号S01和S02粘贴到批次200中的两个项目。&#34;
答案 0 :(得分:0)
有趣的问题。这是你可以看到它的一种方式:
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 < position() and position() <= $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() <= $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>
答案 1 :(得分:0)
您可以在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() <= 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>