我想用以下结构转换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批中的两件物品上。"
您可以在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>
有趣的问题。以下是一种看待它的方法:
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>