这是我的 XML 源代码
<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>12354</UniqueID>
<ExpenseAmount>998.00</ExpenseAmount>
<Misc>
<Date>2023-10-10-07:00</Date>
<BonusAmount>400.00</BonusAmount>
</Misc>
<Misc>
<Date>2023-10-09-07:00</Date>
<BonusAmount>900.00</BonusAmount>
</Misc>
</Worker>
<Worker>
<UniqueID>4567</UniqueID>
<ExpenseAmount>1507.00</ExpenseAmount>
<Misc>
<Date>2023-10-06-07:00</Date>
<BonusAmount>322.00</BonusAmount>
</Misc>
<Misc>
<Date>2023-10-08-07:00</Date>
<BonusAmount>800.00</BonusAmount>
</Misc>
<Misc>
<Date>2023-10-09-07:00</Date>
<BonusAmount>410.00</BonusAmount>
</Misc>
</Worker>
</Workers>
这就是我正在尝试做的事情。
- 整个文档中有4个不同的日期,例如
2023-10-10-07:00
2023-10-09-07:00
和2023-10-06-07:00
2023-10-08-07:00
- 由于我的最终输出应该基于
<Date>
并且我的源 xml 上有 4 个不同的日期,因此我的预期结果应该有 4 个<Records>
节点,并且每个节点包含<Date>
对应于的唯一元素<Worker>
- 但是,有 2 个
<Worker>
节点没有任何<Date>
元素,因此任何<Worker>
没有元素的节点<Date>
都应复制到所有<Records>
节点下
我当前的 XSLT。感谢 michael.hor257k
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/Workers">
<Workers>
<xsl:variable name="undated" select="Worker[not(Misc)]"/>
<xsl:variable name="dated" select="Worker[(Misc)]"/>
<xsl:for-each-group select="Worker/Misc" group-by="Date">
<Records>
<xsl:copy-of select="$undated"/>
<xsl:copy-of select="current-group()"/>
</Records>
</xsl:for-each-group>
</Workers>
</xsl:template>
</xsl:stylesheet>
我目前的结果
<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Records>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Misc>
<Date>2023-10-10-07:00</Date>
<BonusAmount>400.00</BonusAmount>
</Misc>
</Records>
<Records>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Misc>
<Date>2023-10-09-07:00</Date>
<BonusAmount>900.00</BonusAmount>
</Misc>
<Misc>
<Date>2023-10-09-07:00</Date>
<BonusAmount>410.00</BonusAmount>
</Misc>
</Records>
<Records>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Misc>
<Date>2023-10-06-07:00</Date>
<BonusAmount>322.00</BonusAmount>
</Misc>
</Records>
<Records>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Misc>
<Date>2023-10-08-07:00</Date>
<BonusAmount>800.00</BonusAmount>
</Misc>
</Records>
</Workers>
我当前的输出按<Records>
预期有 4 个节点,但<Misc>
--><Date>
没有其父<Worker>
节点,如下所示,这是不正确的。
<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Records>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Misc> <!-- Misc should have been under it's <Worker> node -->
<Date>2023-10-10-07:00</Date>
<BonusAmount>400.00</BonusAmount>
</Misc>
</Records>
</Workers>
预期结果
<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Records>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>12354</UniqueID>
<ExpenseAmount>998.00</ExpenseAmount>
<Misc>
<Date>2023-10-10-07:00</Date>
<BonusAmount>400.00</BonusAmount>
</Misc>
</Worker>
</Records>
<Records>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>12354</UniqueID>
<ExpenseAmount>998.00</ExpenseAmount>
<Misc>
<Date>2023-10-09-07:00</Date>
<BonusAmount>900.00</BonusAmount>
</Misc>
</Worker>
<Worker>
<UniqueID>4567</UniqueID>
<ExpenseAmount>1507.00</ExpenseAmount>
<Misc>
<Date>2023-10-09-07:00</Date>
<BonusAmount>410.00</BonusAmount>
</Misc>
</Worker>
</Records>
<Records>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>4567</UniqueID>
<ExpenseAmount>1507.00</ExpenseAmount>
<Misc>
<Date>2023-10-06-07:00</Date>
<BonusAmount>322.00</BonusAmount>
</Misc>
</Worker>
</Records>
<Records>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>4567</UniqueID>
<ExpenseAmount>1507.00</ExpenseAmount>
<Misc>
<Date>2023-10-08-07:00</Date>
<BonusAmount>800.00</BonusAmount>
</Misc>
</Worker>
</Records>
</Workers>
你能帮我解决这个问题吗?谢谢你!
=================================================== ====
仅编辑以下部分
应用 XSLT michael.hor257k 后的结果 根据最初提供的源 XML 获得预期结果。但是,我的源 XML 上可能有多个<Misc>
相同<Date>
但具有不同<BonusAmount>
下<Worker>
节点的节点,如下所示。
**Source Data**
<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>12354</UniqueID>
<ExpenseAmount>998.00</ExpenseAmount>
<Misc>
<Date>2023-10-10-07:00</Date>
<BonusAmount>400.00</BonusAmount> <!-- Amount is 400 here -->
</Misc>
<Misc>
<Date>2023-10-10-07:00</Date>
<BonusAmount>600.00</BonusAmount> <!-- Amount is 600 here -->
</Misc>
</Worker>
</Workers>
XSLT 产生以下结果
<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Records>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>12354</UniqueID>
<ExpenseAmount>998.00</ExpenseAmount>
<Misc>
<Date>2023-10-10-07:00</Date>
<BonusAmount>400.00</BonusAmount>
</Misc>
</Worker>
<Worker>
<UniqueID>12354</UniqueID>
<ExpenseAmount>998.00</ExpenseAmount>
<Misc>
<Date>2023-10-10-07:00</Date>
<BonusAmount>600.00</BonusAmount>
</Misc>
</Worker>
</Records>
</Workers>
预期结果如下,两个<Misc>
元素应分组在同一<Worker>
节点下
对于获得如下结果的任何帮助,我们表示赞赏。
<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Worker>
<UniqueID>67896</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>678965</UniqueID>
<ExpenseAmount>400.00</ExpenseAmount>
<BonusAmount>230.00</BonusAmount>
</Worker>
<Worker>
<UniqueID>12354</UniqueID>
<ExpenseAmount>998.00</ExpenseAmount>
<Misc>
<Date>2023-10-10-07:00</Date>
<BonusAmount>400.00</BonusAmount>
</Misc>
<Misc>
<Date>2023-10-10-07:00</Date>
<BonusAmount>600.00</BonusAmount>
</Misc>
</Worker>
</Workers>
得到预期结果的最终解决方案是
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/Workers">
<Workers>
<xsl:variable name="undated" select="Worker[not(Misc)]"/>
<xsl:for-each-group select="Worker/Misc" group-by="Date">
<Records>
<xsl:copy-of select="$undated"/>
<Worker>
<xsl:copy-of select="../(UniqueID | ExpenseAmount)"/>
</Worker>
<xsl:for-each select="current-group()">
<xsl:copy-of select="."/>
</xsl:for-each>
</Records>
</xsl:for-each-group>
</Workers>
</xsl:template>
</xsl:stylesheet>