For loop XSL输出中的多行 从下面的XML中,我试图实现这一最终输出 XML 12345 基础知识 DEF ABC银行 123456 DEF银行 789012 制药公司 库存
我想看到的输出是: 12345,ABC,DEF,ABC银行,123456,制药For loop XSL输出中的多行 从下面的XML中,我试图实现这一最终输出 XML 12345 基础知识 DEF ABC银行 123456 DEF银行 789012 制药公司 库存,for-loop,xslt,foreach,For Loop,Xslt,Foreach,我想看到的输出是: 12345,ABC,DEF,ABC银行,123456,制药 DEF银行12345号,789012,库存 因此,基本上每个具有多个值的字段都需要在下一行中填充具有多个值的其他字段。如果有第三个名为“Supply”的组织,那么输出应该是 12345,ABC,DEF,ABC银行,123456,制药 DEF银行12345号,789012,库存 12345,,,,供应 我的头脑不清楚我们如何循环获取数据围绕主键创建模板。使用它在银行账户上运行行生成循环。如果组织项目的数量超过银行账户项
DEF银行12345号,789012,库存
因此,基本上每个具有多个值的字段都需要在下一行中填充具有多个值的其他字段。如果有第三个名为“Supply”的组织,那么输出应该是 12345,ABC,DEF,ABC银行,123456,制药
DEF银行12345号,789012,库存
12345,,,,供应
我的头脑不清楚我们如何循环获取数据围绕
主键创建模板
。使用它在银行账户上运行行生成循环。如果组织
项目的数量超过银行账户
项目的数量,则运行其他行循环。如果有必要避免重复(例如,因为您使用的是标识模板),请在每个非主键
元素上调用一个空模板
From the XML below, I am trying to achieve this final output
XML
<root>
<Primary_Key>12345</Primary_Key>
<First_Name>ABC</First_Name>
<Last_Name>DEF<Last_Name>
<Bank_Account>
<Bank_Name>ABC Bank</Bank_Name>
<Bank_Account_No>123456</Bank_Account_No>
</Bank_Account>
<Bank_Account>
<Bank_Name>DEF Bank</Bank_Name>
<Bank_Account_No>789012</Bank_Account_No>
</Bank_Account>
<Organization>Pharma</Organization>
<Organization>Inventory</Organization>
</root>
,,
;
;
如果使用标识模板,请使用
使其余元素无效。围绕主键创建模板。使用它在银行账户上运行行生成循环。如果组织
项目的数量超过银行账户
项目的数量,则运行其他行循环。如果有必要避免重复(例如,因为您使用的是标识模板),请在每个非主键
元素上调用一个空模板
From the XML below, I am trying to achieve this final output
XML
<root>
<Primary_Key>12345</Primary_Key>
<First_Name>ABC</First_Name>
<Last_Name>DEF<Last_Name>
<Bank_Account>
<Bank_Name>ABC Bank</Bank_Name>
<Bank_Account_No>123456</Bank_Account_No>
</Bank_Account>
<Bank_Account>
<Bank_Name>DEF Bank</Bank_Name>
<Bank_Account_No>789012</Bank_Account_No>
</Bank_Account>
<Organization>Pharma</Organization>
<Organization>Inventory</Organization>
</root>
,,
;
;
如果使用标识模板,请使用
使其余元素无效。这是一个简单的步骤
<transform xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0">
<output method="text"/>
<template match="child::Primary_Key">
<variable name="organizations.outnumber.bank.accounts" select="count(following-sibling::Bank_Account) < count(following-sibling::Organization)"/>
<for-each select="following-sibling::Bank_Account">
<variable name="row.number" select="position()"/>
<value-of select="concat(preceding-sibling::Primary_Key/child::text(), ',')"/>
<choose>
<when test="$row.number = 1">
<value-of select="concat(preceding-sibling::First_Name/child::text(), ',', preceding-sibling::Last_Name/child::text(), ',')"/>
</when>
<otherwise>,,</otherwise>
</choose>
<value-of select="concat(child::Bank_Name/child::text(), ',', child::Bank_Account_No/child::text(), ',')"/>
<if test="count(following-sibling::Organization[position() = $row.number]) = 1">
<value-of select="following-sibling::Organization[position() = $row.number]/child::text()"/>
</if>
<if test="not(self::Bank_Account[position() = last()]) or $organizations.outnumber.bank.accounts">
<text>
</text>
</if>
</for-each>
<if test="$organizations.outnumber.bank.accounts">
<variable name="row.start.number" select="count(following-sibling::Bank_Account) + 1"/>
<for-each select="following-sibling::Organization[position() >= $row.start.number]">
<variable name="row.number" select="position()"/>
<value-of select="concat(preceding-sibling::Primary_Key/child::text(), ',,,,,')"/>
<value-of select="self::Organization[position() = $row.number]/child::text()"/>
<if test="not(self::Organization[position() = last()])">
<text>
</text>
</if>
</for-each>
</if>
</template>
<template match="child::root">
<apply-templates select="child::Primary_Key"/>
</template>
</transform>
;
这是一个简单的步骤
<transform xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0">
<output method="text"/>
<template match="child::Primary_Key">
<variable name="organizations.outnumber.bank.accounts" select="count(following-sibling::Bank_Account) < count(following-sibling::Organization)"/>
<for-each select="following-sibling::Bank_Account">
<variable name="row.number" select="position()"/>
<value-of select="concat(preceding-sibling::Primary_Key/child::text(), ',')"/>
<choose>
<when test="$row.number = 1">
<value-of select="concat(preceding-sibling::First_Name/child::text(), ',', preceding-sibling::Last_Name/child::text(), ',')"/>
</when>
<otherwise>,,</otherwise>
</choose>
<value-of select="concat(child::Bank_Name/child::text(), ',', child::Bank_Account_No/child::text(), ',')"/>
<if test="count(following-sibling::Organization[position() = $row.number]) = 1">
<value-of select="following-sibling::Organization[position() = $row.number]/child::text()"/>
</if>
<if test="not(self::Bank_Account[position() = last()]) or $organizations.outnumber.bank.accounts">
<text>
</text>
</if>
</for-each>
<if test="$organizations.outnumber.bank.accounts">
<variable name="row.start.number" select="count(following-sibling::Bank_Account) + 1"/>
<for-each select="following-sibling::Organization[position() >= $row.start.number]">
<variable name="row.number" select="position()"/>
<value-of select="concat(preceding-sibling::Primary_Key/child::text(), ',,,,,')"/>
<value-of select="self::Organization[position() = $row.number]/child::text()"/>
<if test="not(self::Organization[position() = last()])">
<text>
</text>
</if>
</for-each>
</if>
</template>
<template match="child::root">
<apply-templates select="child::Primary_Key"/>
</template>
</transform>
;
我比我更喜欢这个解决方案,尽管它假设组织
元素的数量总是超过银行账户
元素,而这些元素对于OP的代码可能是真的,也可能不是真的。它还在开始时输出一个不必要的新行字符,可以用
修复。为了适应@PatrickDark指出的内容,我在开始时计算每个字段的计数,并使用for循环中的最高计数。我更喜欢这个解决方案,尽管它假定组织
元素的数量总是超过银行账户
元素,这对于OP的代码来说可能是真的,也可能不是真的。它还在开始处输出一个不必要的新行字符,可以用
修复。为了适应@PatrickDark指出的内容,我在开始处计算每个字段的计数,并使用for循环中的最高计数。“基本上,每个具有多个值的字段都需要在下一行填充具有多个值的其他字段。”您需要澄清是否有一个字段(例如,组织
)始终具有最大值。”基本上,具有多个值的每个字段都需要在下一行填充具有多个值的其他字段。“您需要澄清是否有一个字段(例如,组织
)始终具有最大值。