迭代具有相同值的子节点 - XSLT
Iterate over Child-Nodes with Same Value - XSLT
想知道是否可以使用 XSLT 实现以下目标:
我需要遍历 XML 树的子节点,并将字段 auditid 标记中具有相似值的节点组合在一起。要求是为每个具有相同 auditid 的“记录”加入“附件”节点。
示例输入XML:
<?xml version='1.0' encoding='UTF-8'?>
<Root>
<total_count>53</total_count>
<records>
<auditid>00000001</auditid>
<completationdate>2021-08-04</completationdate>
<organization>testcompany1</organization>
<streetaddress>1 peel street, melbourne</streetaddress>
<suburb>South Melbourne</suburb>
</records>
<records>
<auditid>00000002</auditid>
<completationdate>2021-07-07</completationdate>
<organization>testcompany2</organization>
<streetaddress>1 test road, West Footscray</streetaddress>
<suburb>West Footscray</suburb>
<attachments>
<label>71270-2_A</label>
<url>http://test/53X/00000002/71270-2_A.pdf</url>
</attachments>
</records>
<records>
<auditid>00000002</auditid>
<completationdate>2021-07-07</completationdate>
<organization>testcompany2</organization>
<streetaddress>1 test road, West Footscray</streetaddress>
<suburb>West Footscray</suburb>
<attachments>
<label>71270-2_B</label>
<url>http://test/53X/00000002-2_B.pdf</url>
</attachments>
</records>
<records>
<auditid>00000002</auditid>
<completationdate>2021-07-07</completationdate>
<organization>testcompany2</organization>
<streetaddress>1 test road, West Footscray</streetaddress>
<suburb>West Footscray</suburb>
<attachments>
<label>71270-2_C</label>
<url>http://test/53X/00000002/71270-2_C.pdf</url>
</attachments>
</records>
<records>
<auditid>00000003</auditid>
<completationdate>2021-07-08</completationdate>
<organization>testcompany3</organization>
<streetaddress>1 test road, Hampton Park</streetaddress>
<suburb>Hampton Park</suburb>
<attachments>
<label>60797-25_A</label>
<url>http://test/53V/00000003/60797-25_A.pdf</url>
</attachments>
</records>
<records>
<auditid>00000003</auditid>
<completationdate>2021-07-08</completationdate>
<organization>testcompany3</organization>
<streetaddress>1 test road, Hampton Park</streetaddress>
<suburb>Hampton Park</suburb>
<attachments>
<label>60797-25_B</label>
<url>http://test/53V/00000003/60797-25_B.pdf</url>
</attachments>
</records>
</Root>
我的 XSLT:
到目前为止,我已经能够生成每个具有相同值的 auditid 的计数。但无法弄清楚如何迭代这些。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="dogo" match="Root/records" use="auditid" />
<xsl:template match="/Root">
<root>
<total_count>
<xsl:value-of select="total_count"/>
</total_count>
<xsl:for-each select="records[count(key('dogo', auditid)) = 1]">
<xsl:variable name="count">
<xsl:value-of select="count(key('dogo', auditid))"/>
</xsl:variable>
<records>
<count>
<xsl:value-of select="$count"/>
</count>
<xsl:copy-of select="auditid"/>
<xsl:copy-of select="completationdate"/>
<xsl:copy-of select="organization"/>
<xsl:copy-of select="streetaddress"/>
<xsl:copy-of select="suburb"/>
<xsl:copy-of select="attachments"/>
</records>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
预期输出:
<?xml version='1.0' encoding='UTF-8'?>
<Root>
<total_count>53</total_count>
<records>
<auditid>00000001</auditid>
<completationdate>2021-07-20</completationdate>
<organization>testcompany1</organization>
<streetaddress>1 peel street, melbourne</streetaddress>
<suburb>South Melbourne</suburb>
</records>
<records>
<auditid>00000002</auditid>
<completationdate>2021-07-07</completationdate>
<organization>testcompany2</organization>
<streetaddress>1 test road, West Footscray</streetaddress>
<suburb>West Footscray</suburb>
<attachments>
<label>71270-2_A</label>
<url>http://test/53X/00000002/71270-2_A.pdf</url>
</attachments>
<attachments>
<label>71270-2_B</label>
<url>http://test/53X/00000002-2_B.pdf</url>
</attachments>
<attachments>
<label>71270-2_C</label>
<url>http://test/53X/00000002/71270-2_C.pdf</url>
</attachments>
</records>
<records>
<auditid>00000003</auditid>
<completationdate>2021-07-08</completationdate>
<organization>testcompany3</organization>
<streetaddress>1 test road, Hampton Park</streetaddress>
<suburb>Hampton Park</suburb>
<attachments>
<label>60797-25_A</label>
<url>http://test/53V/00000003/60797-25_A.pdf</url>
</attachments>
<attachments>
<label>60797-25_B</label>
<url>http://test/53V/00000003/60797-25_B.pdf</url>
</attachments>
</records>
</Root>
如果您能够使用 XSLT 2.0 那么为什么不利用它的 grouping 功能并简单地做:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Root">
<xsl:copy>
<xsl:copy-of select="total_count"/>
<xsl:for-each-group select="records" group-by="auditid">
<records>
<xsl:copy-of select="* except attachments"/>
<xsl:copy-of select="current-group()/attachments"/>
</records>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
想知道是否可以使用 XSLT 实现以下目标: 我需要遍历 XML 树的子节点,并将字段 auditid 标记中具有相似值的节点组合在一起。要求是为每个具有相同 auditid 的“记录”加入“附件”节点。
示例输入XML:
<?xml version='1.0' encoding='UTF-8'?>
<Root>
<total_count>53</total_count>
<records>
<auditid>00000001</auditid>
<completationdate>2021-08-04</completationdate>
<organization>testcompany1</organization>
<streetaddress>1 peel street, melbourne</streetaddress>
<suburb>South Melbourne</suburb>
</records>
<records>
<auditid>00000002</auditid>
<completationdate>2021-07-07</completationdate>
<organization>testcompany2</organization>
<streetaddress>1 test road, West Footscray</streetaddress>
<suburb>West Footscray</suburb>
<attachments>
<label>71270-2_A</label>
<url>http://test/53X/00000002/71270-2_A.pdf</url>
</attachments>
</records>
<records>
<auditid>00000002</auditid>
<completationdate>2021-07-07</completationdate>
<organization>testcompany2</organization>
<streetaddress>1 test road, West Footscray</streetaddress>
<suburb>West Footscray</suburb>
<attachments>
<label>71270-2_B</label>
<url>http://test/53X/00000002-2_B.pdf</url>
</attachments>
</records>
<records>
<auditid>00000002</auditid>
<completationdate>2021-07-07</completationdate>
<organization>testcompany2</organization>
<streetaddress>1 test road, West Footscray</streetaddress>
<suburb>West Footscray</suburb>
<attachments>
<label>71270-2_C</label>
<url>http://test/53X/00000002/71270-2_C.pdf</url>
</attachments>
</records>
<records>
<auditid>00000003</auditid>
<completationdate>2021-07-08</completationdate>
<organization>testcompany3</organization>
<streetaddress>1 test road, Hampton Park</streetaddress>
<suburb>Hampton Park</suburb>
<attachments>
<label>60797-25_A</label>
<url>http://test/53V/00000003/60797-25_A.pdf</url>
</attachments>
</records>
<records>
<auditid>00000003</auditid>
<completationdate>2021-07-08</completationdate>
<organization>testcompany3</organization>
<streetaddress>1 test road, Hampton Park</streetaddress>
<suburb>Hampton Park</suburb>
<attachments>
<label>60797-25_B</label>
<url>http://test/53V/00000003/60797-25_B.pdf</url>
</attachments>
</records>
</Root>
我的 XSLT: 到目前为止,我已经能够生成每个具有相同值的 auditid 的计数。但无法弄清楚如何迭代这些。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="dogo" match="Root/records" use="auditid" />
<xsl:template match="/Root">
<root>
<total_count>
<xsl:value-of select="total_count"/>
</total_count>
<xsl:for-each select="records[count(key('dogo', auditid)) = 1]">
<xsl:variable name="count">
<xsl:value-of select="count(key('dogo', auditid))"/>
</xsl:variable>
<records>
<count>
<xsl:value-of select="$count"/>
</count>
<xsl:copy-of select="auditid"/>
<xsl:copy-of select="completationdate"/>
<xsl:copy-of select="organization"/>
<xsl:copy-of select="streetaddress"/>
<xsl:copy-of select="suburb"/>
<xsl:copy-of select="attachments"/>
</records>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
预期输出:
<?xml version='1.0' encoding='UTF-8'?>
<Root>
<total_count>53</total_count>
<records>
<auditid>00000001</auditid>
<completationdate>2021-07-20</completationdate>
<organization>testcompany1</organization>
<streetaddress>1 peel street, melbourne</streetaddress>
<suburb>South Melbourne</suburb>
</records>
<records>
<auditid>00000002</auditid>
<completationdate>2021-07-07</completationdate>
<organization>testcompany2</organization>
<streetaddress>1 test road, West Footscray</streetaddress>
<suburb>West Footscray</suburb>
<attachments>
<label>71270-2_A</label>
<url>http://test/53X/00000002/71270-2_A.pdf</url>
</attachments>
<attachments>
<label>71270-2_B</label>
<url>http://test/53X/00000002-2_B.pdf</url>
</attachments>
<attachments>
<label>71270-2_C</label>
<url>http://test/53X/00000002/71270-2_C.pdf</url>
</attachments>
</records>
<records>
<auditid>00000003</auditid>
<completationdate>2021-07-08</completationdate>
<organization>testcompany3</organization>
<streetaddress>1 test road, Hampton Park</streetaddress>
<suburb>Hampton Park</suburb>
<attachments>
<label>60797-25_A</label>
<url>http://test/53V/00000003/60797-25_A.pdf</url>
</attachments>
<attachments>
<label>60797-25_B</label>
<url>http://test/53V/00000003/60797-25_B.pdf</url>
</attachments>
</records>
</Root>
如果您能够使用 XSLT 2.0 那么为什么不利用它的 grouping 功能并简单地做:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Root">
<xsl:copy>
<xsl:copy-of select="total_count"/>
<xsl:for-each-group select="records" group-by="auditid">
<records>
<xsl:copy-of select="* except attachments"/>
<xsl:copy-of select="current-group()/attachments"/>
</records>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>