我正在嘗試從簡單 XML 創建嵌套 XML。但無法弄清楚如何對相同的元素進行分組和區分。此外,如何使用 for-each-group 在 XSLT 中添加第二級嵌套。
下面是我的輸入 XML:
<?xml version="1.0" encoding="UTF-8"?>
<!--XML document created Oct 28, 2022 9:43:47 AM-->
<dataroot>
<CTMS_Simple>
<EmployeeID>EmployeeID 1</EmployeeID>
<DEPT_REFERENCE>DEPT 1</DEPT_REFERENCE>
<DEPT_CODE>DEPTCC 1</DEPT_CODE>
<DEPT_NAME>DEPTDN 1</DEPT_NAME>
<LocationID>Location 1</LocationID>
</CTMS_Simple>
<CTMS_Simple>
<EmployeeID>EmployeeID 1</EmployeeID>
<DEPT_REFERENCE>DEPT 1</DEPT_REFERENCE>
<DEPT_CODE>DEPTCC 1</DEPT_CODE>
<DEPT_NAME>DEPTDN 1</DEPT_NAME>
<LocationID>Location 2</LocationID>
</CTMS_Simple>
<CTMS_Simple>
<EmployeeID>EmployeeID 2</EmployeeID>
<DEPT_REFERENCE>DEPT 1</DEPT_REFERENCE>
<DEPT_CODE>DEPTCC 1</DEPT_CODE>
<DEPT_NAME>DEPTDN 3</DEPT_NAME>
<LocationID>Location 1</LocationID>
</CTMS_Simple>
<CTMS_Simple>
<EmployeeID>EmployeeID 3</EmployeeID>
<DEPT_REFERENCE>DEPT 2</DEPT_REFERENCE>
<DEPT_CODE>DEPTCC 2</DEPT_CODE>
<DEPT_NAME>DEPTDN 2</DEPT_NAME>
<LocationID>Location 4</LocationID>
</CTMS_Simple>
</dataroot>
我在 XSLT v2.0 檔案下創建:我無法在樣式表中包含位置資料。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs xsi xsl">
<xsl:template match="/">
<xsl:for-each-group select="/dataroot/CTMS_Simple"
group-by="DEPT_REFERENCE">
<Department>
<DEPTID><xsl:value-of select="DEPT_REFERENCE" /></DEPTID>
<DEPTCODE><xsl:value-of select="DEPT_CODE" /></DEPTCODE>
<DeptName><xsl:value-of select="DEVELOPMENT_NAME" /></DeptName>
<xsl:for-each select="/dataroot/CTMS_Simple/EmployeeID[../DEPT_REFERENCE=current-grouping-key()]">
<Employee>
<EmployeeID>
<xsl:value-of select="." />
</EmployeeID>
</Employee>
</xsl:for-each>
</Department>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
我得到以下輸出:
<Department>
<DEPTID>DEPT 1</DEPTID>
<DEPTCODE>DEPTCC 1</DEPTCODE>
<DeptName/>
<Employee>
<EmployeeID>EmployeeID 1</EmployeeID>
</Employee>
<Employee>
<EmployeeID>EmployeeID 1</EmployeeID>
</Employee>
<Employee>
<EmployeeID>EmployeeID 2</EmployeeID>
</Employee>
</Department>
<Department>
<DEPTID>DEPT 2</DEPTID>
<DEPTCODE>DEPTCC 2</DEPTCODE>
<DeptName/>
<Employee>
<EmployeeID>EmployeeID 3</EmployeeID>
</Employee>
</Department>
但是在添加位置資料后,我需要如下輸出。
<Department>
<DEPTID>DEPT 1</DEPTID>
<DEPTCODE>DEPTCC 1</DEPTCODE>
<DeptName/>
<Employee>
<EmployeeID>EmployeeID 1</EmployeeID>
<Location>
<LocationID>Location 1</LocationID>
<LocationID>Location 2</LocationID>
</Location>
</Employee>
<Employee>
<EmployeeID>EmployeeID 2</EmployeeID>
<Location>
<LocationID>Location 1</LocationID>
</Location>
</Employee>
</Department>
<Department>
<DEPTID>DEPT 2</DEPTID>
<DEPTCODE>DEPTCC 2</DEPTCODE>
<DeptName/>
<Employee>
<EmployeeID>EmployeeID 3</EmployeeID>
<Location>
<LocationID>Location 4</LocationID>
</Location>
</Employee>
</Department>
uj5u.com熱心網友回復:
我猜你想要類似的東西:
<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="/dataroot">
<xsl:for-each-group select="CTMS_Simple" group-by="DEPT_REFERENCE">
<Department>
<DEPTID>
<xsl:value-of select="DEPT_REFERENCE" />
</DEPTID>
<DEPTCODE>
<xsl:value-of select="DEPT_CODE" />
</DEPTCODE>
<DeptName>
<xsl:value-of select="DEVELOPMENT_NAME" />
</DeptName>
<xsl:for-each-group select="current-group()" group-by="EmployeeID">
<Employee>
<EmployeeID>
<xsl:value-of select="EmployeeID" />
</EmployeeID>
<Location>
<xsl:for-each select="current-group()">
<LocationID>
<xsl:value-of select="LocationID" />
</LocationID>
</xsl:for-each>
</Location>
</Employee>
</xsl:for-each-group>
</Department>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
請注意,結果是一個 XML 片段,而不是格式良好的 XML 檔案(沒有單個根元素)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/525011.html
標籤:xmlxslt
