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>
我想看的输出是:
12345,ABC,DEF,ABC Bank,123456,Pharma
12345 ,,, DEF Bank,789012,库存
因此,基本上每个具有多个值的字段都需要在下一行填充其他具有多个值的字段。如果有一个名为“Supply”的第三个组织,那么输出应该是
12345,ABC,DEF,ABC Bank,123456,Pharma
12345 ,,, DEF Bank,789012,库存
12345 ,,,,,供应
无法理解我们如何循环获取数据
答案 0 :(得分:2)
这是一个简单的步骤
<xsl:template match="/">
<xsl:for-each select="//Organization">
<xsl:variable name="pos" select="position()"/>
<xsl:text>
</xsl:text>
<xsl:value-of select="concat(parent::root/Primary_Key, ',' , parent::root/First_Name[$pos], ',' , parent::root/Last_Name[$pos], ',' , parent::root/Bank_Account[$pos]/Bank_Name, ',' , parent::root/Bank_Account[$pos]/Bank_Account_No, ',', current())"/>
</xsl:for-each>
</xsl:template>
答案 1 :(得分:1)
围绕public static int returnIndex(int[] haystack, int needle) {
// iterates each value of haystack
for (int n : haystack) {
// this checks for a value at index needle, if needle were 300 it
// looks for haystack[300]
// if haystack were a 10 index array it would lead to an error
if (haystack[needle] == n) {
}
// using this else statement will lead to exiting program
// the first time `haystack[needle] == n` is false this function
// finishes
else {
System.out.println("Element not found in array");
System.exit(0);
}
}
return needle;
}
创建模板。使用它在Primary_Key
上运行行生成循环。如果Bank_Account
项的数量超过Organization
项的数量,请运行其他行循环。如有必要避免重复(例如,因为您使用的是身份模板),请在每个非Bank_Account
元素上调用空模板。
Primary_Key
如果使用身份模板,请使用<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>
取消其余元素。