参考我之前的帖子Limit iteration of inner for-each loop in XSLT,询问有关映射到特定字段而不是使用元素的其他问题。
输入XML:
<?xml version="1.0" encoding="utf-8"?>
<Loop_Pro>
<Pro>
<ProID>PRO101</ProID>
<Loop_Sub>
<Sub>
<SubID>SUB101</SubID>
<Loop_CLM>
<CLM>
<CLMID>CLM101</CLMID>
<nextfield1>CLM101N1</nextfield1>
<nextfield2>CLM101N2</nextfield2>
</CLM>
<CLM>
<CLMID>CLM102</CLMID>
<nextfield1>CLM102N1</nextfield1>
<nextfield2>CLM102N2</nextfield2>
</CLM>
<CLM>
<CLMID>CLM103</CLMID>
<nextfield1>CLM103N1</nextfield1>
<nextfield2>CLM103N2</nextfield2>
</CLM>
<CLM>
<CLMID>CLM104</CLMID>
<nextfield1>CLM104N1</nextfield1>
<nextfield2>CLM104N2</nextfield2>
</CLM>
</Loop_CLM>
</Sub>
<Sub>
<SubID>SUB102</SubID>
<Loop_CLM>
<CLM>
<CLMID>CLM201</CLMID>
<nextfield1>CLM201N1</nextfield1>
<nextfield2>CLM201N2</nextfield2>
</CLM>
<CLM>
<CLMID>CLM202</CLMID>
<nextfield1>CLM202N1</nextfield1>
<nextfield2>CLM202N2</nextfield2>
</CLM>
<CLM>
<CLMID>CLM203</CLMID>
<nextfield1>CLM203N1</nextfield1>
<nextfield2>CLM203N2</nextfield2>
</CLM>
</Loop_CLM>
</Sub>
<Sub>
<SubID>SUB103</SubID>
<Loop_CLM>
<CLM>
<CLMID>CLM301</CLMID>
<nextfield1>CLM301N1</nextfield1>
<nextfield2>CLM301N2</nextfield2>
</CLM>
<CLM>
<CLMID>CLM302</CLMID>
<nextfield1>CLM302N1</nextfield1>
<nextfield2>CLM302N2</nextfield2>
</CLM>
<CLM>
<CLMID>CLM303</CLMID>
<nextfield1>CLM303N1</nextfield1>
<nextfield2>CLM303N2</nextfield2>
</CLM>
<CLM>
<CLMID>CLM304</CLMID>
<nextfield1>CLM304N1</nextfield1>
<nextfield2>CLM304N2</nextfield2>
</CLM>
<CLM>
<CLMID>CLM305</CLMID>
<nextfield1>CLM305N1</nextfield1>
<nextfield2>CLM305N2</nextfield2>
</CLM>
</Loop_CLM>
</Sub>
</Loop_Sub>
</Pro>
<Pro>
<ProID>PRO102</ProID>
<Loop_Sub>
<Sub>
<SubID>SUB202</SubID>
<Loop_CLM>
<CLM>
<CLMID>CLM201</CLMID>
<nextfield1>CLM101N1</nextfield1>
<nextfield2>CLM101N2</nextfield2>
</CLM>
</Loop_CLM>
</Sub>
</Loop_Sub>
</Pro>
</Loop_Pro>
XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
<xsl:strip-space elements="*"/>
<xsl:variable name="CLMGroupOccur">2</xsl:variable>
<xsl:template match="/Loop_Pro">
<OB_X1>
<xsl:for-each select="Pro">
<xsl:copy>
<GivenProID>
<xsl:value-of select="ProID"/>
</GivenProID>
<xsl:for-each select="Loop_Sub">
<LoopSub>
<xsl:for-each select="Sub">
<xsl:call-template name="LoopCLM">
<xsl:with-param name="pos" select="1"/>
</xsl:call-template>
</xsl:for-each>
</LoopSub>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
</OB_X1>
</xsl:template>
<xsl:template name="LoopCLM">
<xsl:param name="pos"/>
<GivenSubID>
<xsl:value-of select="SubID"/>
</GivenSubID>
<LoopCLM>
<xsl:copy-of select="Loop_CLM/CLM[position() <= $pos + $CLMGroupOccur - 1 and position() >= $pos]"/>
</LoopCLM>
<xsl:if test="Loop_CLM/CLM[position() = $pos + $CLMGroupOccur]">
<xsl:call-template name="LoopCLM">
<xsl:with-param name="pos" select="$pos + $CLMGroupOccur"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
输出XML - 实际:
<OB_X1>
<Pro>
<GivenProID>PRO101</GivenProID>
<LoopSub>
<GivenSubID>SUB101</GivenSubID>
<LoopCLM>
<CLM>
<CLMID>CLM101</CLMID>
<nextfield1>CLM101N1</nextfield1>
<nextfield2>CLM101N2</nextfield2>
</CLM>
<CLM>
<CLMID>CLM102</CLMID>
<nextfield1>CLM102N1</nextfield1>
<nextfield2>CLM102N2</nextfield2>
</CLM>
</LoopCLM>
<GivenSubID>SUB101</GivenSubID>
<LoopCLM>
<CLM>
<CLMID>CLM103</CLMID>
<nextfield1>CLM103N1</nextfield1>
<nextfield2>CLM103N2</nextfield2>
</CLM>
<CLM>
<CLMID>CLM104</CLMID>
<nextfield1>CLM104N1</nextfield1>
<nextfield2>CLM104N2</nextfield2>
</CLM>
</LoopCLM>
<GivenSubID>SUB102</GivenSubID>
<LoopCLM>
<CLM>
<CLMID>CLM201</CLMID>
<nextfield1>CLM201N1</nextfield1>
<nextfield2>CLM201N2</nextfield2>
</CLM>
<CLM>
<CLMID>CLM202</CLMID>
<nextfield1>CLM202N1</nextfield1>
<nextfield2>CLM202N2</nextfield2>
</CLM>
</LoopCLM>
<GivenSubID>SUB102</GivenSubID>
<LoopCLM>
<CLM>
<CLMID>CLM203</CLMID>
<nextfield1>CLM203N1</nextfield1>
<nextfield2>CLM203N2</nextfield2>
</CLM>
</LoopCLM>
<GivenSubID>SUB103</GivenSubID>
<LoopCLM>
<CLM>
<CLMID>CLM301</CLMID>
<nextfield1>CLM301N1</nextfield1>
<nextfield2>CLM301N2</nextfield2>
</CLM>
<CLM>
<CLMID>CLM302</CLMID>
<nextfield1>CLM302N1</nextfield1>
<nextfield2>CLM302N2</nextfield2>
</CLM>
</LoopCLM>
<GivenSubID>SUB103</GivenSubID>
<LoopCLM>
<CLM>
<CLMID>CLM303</CLMID>
<nextfield1>CLM303N1</nextfield1>
<nextfield2>CLM303N2</nextfield2>
</CLM>
<CLM>
<CLMID>CLM304</CLMID>
<nextfield1>CLM304N1</nextfield1>
<nextfield2>CLM304N2</nextfield2>
</CLM>
</LoopCLM>
<GivenSubID>SUB103</GivenSubID>
<LoopCLM>
<CLM>
<CLMID>CLM305</CLMID>
<nextfield1>CLM305N1</nextfield1>
<nextfield2>CLM305N2</nextfield2>
</CLM>
</LoopCLM>
</LoopSub>
</Pro>
<Pro>
<GivenProID>PRO102</GivenProID>
<LoopSub>
<GivenSubID>SUB202</GivenSubID>
<LoopCLM>
<CLM>
<CLMID>CLM201</CLMID>
<nextfield1>CLM101N1</nextfield1>
<nextfield2>CLM101N2</nextfield2>
</CLM>
</LoopCLM>
</LoopSub>
</Pro>
</OB_X1>
所需的输出XML:
<OB_X1>
<Pro>
<GivenProID>PRO101</GivenProID>
<LoopSub>
<GivenSubID>SUB101</GivenSubID>
<LoopCLM>
<CLM>
<CLMID>CLM101</CLMID>
<OutFreqQual>CLM101N1</OutFreqQual>
<OutFreqID>CLM101N2</OutFreqID>
</CLM>
<CLM>
<CLMID>CLM102</CLMID>
<OutFreqQual>CLM102N1</OutFreqQual>
<OutFreqID>CLM102N2</OutFreqID>
</CLM>
</LoopCLM>
<GivenSubID>SUB101</GivenSubID>
<LoopCLM>
<CLM>
<CLMID>CLM103</CLMID>
<OutFreqQual>CLM103N1</OutFreqQual>
<OutFreqID>CLM103N2</OutFreqID>
</CLM>
<CLM>
<CLMID>CLM104</CLMID>
<OutFreqQual>CLM104N1</OutFreqQual>
<OutFreqID>CLM104N2</OutFreqID>
</CLM>
</LoopCLM>
<GivenSubID>SUB102</GivenSubID>
<LoopCLM>
<CLM>
<CLMID>CLM201</CLMID>
<OutFreqQual>CLM201N1</OutFreqQual>
<OutFreqID>CLM201N2</OutFreqID>
</CLM>
<CLM>
<CLMID>CLM202</CLMID>
<OutFreqQual>CLM202N1</OutFreqQual>
<OutFreqID>CLM202N2</OutFreqID>
</CLM>
</LoopCLM>
<GivenSubID>SUB102</GivenSubID>
<LoopCLM>
<CLM>
<CLMID>CLM203</CLMID>
<OutFreqQual>CLM203N1</OutFreqQual>
<OutFreqID>CLM203N2</OutFreqID>
</CLM>
</LoopCLM>
<GivenSubID>SUB103</GivenSubID>
<LoopCLM>
<CLM>
<CLMID>CLM301</CLMID>
<OutFreqQual>CLM301N1</OutFreqQual>
<OutFreqID>CLM301N2</OutFreqID>
</CLM>
<CLM>
<CLMID>CLM302</CLMID>
<OutFreqQual>CLM302N1</OutFreqQual>
<OutFreqID>CLM302N2</OutFreqID>
</CLM>
</LoopCLM>
<GivenSubID>SUB103</GivenSubID>
<LoopCLM>
<CLM>
<CLMID>CLM303</CLMID>
<OutFreqQual>CLM303N1</OutFreqQual>
<OutFreqID>CLM303N2</OutFreqID>
</CLM>
<CLM>
<CLMID>CLM304</CLMID>
<OutFreqQual>CLM304N1</OutFreqQual>
<OutFreqID>CLM304N2</OutFreqID>
</CLM>
</LoopCLM>
<GivenSubID>SUB103</GivenSubID>
<LoopCLM>
<CLM>
<CLMID>CLM305</CLMID>
<OutFreqQual>CLM305N1</OutFreqQual>
<OutFreqID>CLM305N2</OutFreqID>
</CLM>
</LoopCLM>
</LoopSub>
</Pro>
<Pro>
<GivenProID>PRO102</GivenProID>
<LoopSub>
<GivenSubID>SUB202</GivenSubID>
<LoopCLM>
<CLM>
<CLMID>CLM201</CLMID>
<OutFreqQual>CLM101N1</OutFreqQual>
<OutFreqID>CLM101N2</OutFreqID>
</CLM>
</LoopCLM>
</LoopSub>
</Pro>
</OB_X1>
任何想法,如何用特定目标字段替换<xsl:copy>
。感谢任何帮助
谢谢, 哈里什
答案 0 :(得分:0)
对您当前方法的最简单修改是添加一个匹配CLM
元素的模板并进行相应的重命名:
<xsl:template match="CLM">
<CLM>
<xsl:copy-of select="CLMID"/>
<OutFreqQual><xsl:value-of select="nextfield1"/></OutFreqQual>
<OutFreqID><xsl:value-of select="nextfield2"/></OutFreqID>
</CLM>
</xsl:template>
然后将copy-of
替换为apply-templates
:
<xsl:apply-templates select="Loop_CLM/CLM[position() <= $pos + $CLMGroupOccur - 1 and position() >= $pos]"/>
同样的方法可以作为the alternative solution to your previous question的修改。