XSLT 中的条件分组
Conditional grouping in XSLT
请求您帮助使用 XSLT 2
转换 XML
我的输入 XML 如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Worker>
<Batch_number>1234</Batch_number>
<Name>David</Name>
<Assignment>
<type>A</type>
<name>New York</name>
</Assignment>
</Worker>
<Worker>
<Batch_number>1234</Batch_number>
<Name>David</Name>
<Assignment>
<type>B</type>
<name>Boston</name>
</Assignment>
</Worker>
<Worker>
<Batch_number>1234</Batch_number>
<Name>David</Name>
<Assignment>
<type>C</type>
<name>Chicago</name>
</Assignment>
</Worker>
<Worker>
<Batch_number>78698</Batch_number>
<Name>Karen</Name>
<Assignment>
<type>C</type>
<name>Chicago</name>
</Assignment>
</Worker>
</Workers>
所需的输出如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<Worker>
<Batch_number>1234</Batch_number>
<Name>David</Name>
<Assignment>
<type>A</type>
<name>New York</name>
</Assignment>
<Assignment>
<type>B</type>
<name>Boston</name>
</Assignment>
<Assignment>
<type>C</type>
<name>Chicago</name>
</Assignment>
</Worker>
<Worker>
<Batch_number>78698</Batch_number>
<Name>Karen</Name>
<Assignment>
<type>C</type>
<name>Chicago</name>
</Assignment>
</Worker>
转换条件为:
如果输入文件中存在具有相同 <Batch_number>
的子节点 <Worker>
,则相同 <Batch_number>
的所有 Assignment
节点应归为一个 Worker
<Batch_number>
和 <Name>
之后的子节点
有人可以帮我写正确的代码来实现这个吗?
虽然您想要的输出 XML 不是 格式良好的 ,但这里有一个 XSLT-2.0 解决方案可以满足您的特殊需求:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/Workers">
<xsl:for-each-group select="Worker" group-by="Batch_number">
<Worker>
<Batch_number><xsl:value-of select="current-grouping-key()" /></Batch_number>
<Name><xsl:value-of select="current()/Name" /></Name>
<xsl:copy-of select="current-group()/Assignment" />
</Worker>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
它的输出是:
<?xml version="1.0" encoding="UTF-8"?>
<Worker>
<Batch_number>1234</Batch_number>
<Name>David</Name>
<Assignment>
<type>A</type>
<name>New York</name>
</Assignment>
<Assignment>
<type>B</type>
<name>Boston</name>
</Assignment>
<Assignment>
<type>C</type>
<name>Chicago</name>
</Assignment>
</Worker>
<Worker>
<Batch_number>78698</Batch_number>
<Name>Karen</Name>
<Assignment>
<type>C</type>
<name>Chicago</name>
</Assignment>
</Worker>
请求您帮助使用 XSLT 2
转换 XML我的输入 XML 如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Worker>
<Batch_number>1234</Batch_number>
<Name>David</Name>
<Assignment>
<type>A</type>
<name>New York</name>
</Assignment>
</Worker>
<Worker>
<Batch_number>1234</Batch_number>
<Name>David</Name>
<Assignment>
<type>B</type>
<name>Boston</name>
</Assignment>
</Worker>
<Worker>
<Batch_number>1234</Batch_number>
<Name>David</Name>
<Assignment>
<type>C</type>
<name>Chicago</name>
</Assignment>
</Worker>
<Worker>
<Batch_number>78698</Batch_number>
<Name>Karen</Name>
<Assignment>
<type>C</type>
<name>Chicago</name>
</Assignment>
</Worker>
</Workers>
所需的输出如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<Worker>
<Batch_number>1234</Batch_number>
<Name>David</Name>
<Assignment>
<type>A</type>
<name>New York</name>
</Assignment>
<Assignment>
<type>B</type>
<name>Boston</name>
</Assignment>
<Assignment>
<type>C</type>
<name>Chicago</name>
</Assignment>
</Worker>
<Worker>
<Batch_number>78698</Batch_number>
<Name>Karen</Name>
<Assignment>
<type>C</type>
<name>Chicago</name>
</Assignment>
</Worker>
转换条件为:
如果输入文件中存在具有相同 <Batch_number>
的子节点 <Worker>
,则相同 <Batch_number>
的所有 Assignment
节点应归为一个 Worker
<Batch_number>
和 <Name>
有人可以帮我写正确的代码来实现这个吗?
虽然您想要的输出 XML 不是 格式良好的 ,但这里有一个 XSLT-2.0 解决方案可以满足您的特殊需求:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/Workers">
<xsl:for-each-group select="Worker" group-by="Batch_number">
<Worker>
<Batch_number><xsl:value-of select="current-grouping-key()" /></Batch_number>
<Name><xsl:value-of select="current()/Name" /></Name>
<xsl:copy-of select="current-group()/Assignment" />
</Worker>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
它的输出是:
<?xml version="1.0" encoding="UTF-8"?>
<Worker>
<Batch_number>1234</Batch_number>
<Name>David</Name>
<Assignment>
<type>A</type>
<name>New York</name>
</Assignment>
<Assignment>
<type>B</type>
<name>Boston</name>
</Assignment>
<Assignment>
<type>C</type>
<name>Chicago</name>
</Assignment>
</Worker>
<Worker>
<Batch_number>78698</Batch_number>
<Name>Karen</Name>
<Assignment>
<type>C</type>
<name>Chicago</name>
</Assignment>
</Worker>