当我已经对它进行了ID转换时,我很难停止XSLT转换。我已经知道一旦转换避免重复,如何组合缺失的部分。
任何帮助都会非常感谢,谢谢!
MY XML FILE
?xml version="1.0" encoding="UTF-8"?>
<row>
<data>
<id>1</id>
<fname>Vcut</fname>
<lname>Piatos</lname>
<age>0</age>
<code>1002</code>
<hobby>dota</hobby>
</data>
<data>
<id>1</id>
<fname>Mayo</fname>
<lname>Naise</lname>
<age>22</age>
<code>1003</code>
<hobby>program</hobby>
</data>
<data>
<id>2</id>
<fname>Ben</fname>
<lname>Ten</lname>
<age>22</age>
<code>1003</code>
<hobby>Eat</hobby>
</data>
</row>
XSLT TRANSLATION
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="row/data">
<EmployeeID>
<xsl:for-each-group select="id" group-by="id">
<xsl:value-of select="current-grouping-key()"/>
</xsl:for-each-group>
</EmployeeID>
<EmployeeName>
<xsl:value-of select='concat(fname," ",lname)' />
</EmployeeName>
<EmployeeAge>
<xsl:value-of select='age'/>
</EmployeeAge>
<EmployeeCode><xsl:value-of select="code"/></EmployeeCode>
<EmployeeActivity><xsl:value-of select="hobby"/></EmployeeActivity>
------------------------------------------------------------------------
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
输出
<?xml version="1.0" encoding="UTF-8"?>
<EmployeeID>1</EmployeeID>
<EmployeeName>Vcut Piatos</EmployeeName>
<EmployeeAge>0</EmployeeAge>
<EmployeeCode>1002</EmployeeCode>
<EmployeeActivity>dota</EmployeeActivity>
------------------------------------------------------------------------
<EmployeeID/>
<EmployeeName>Mayo Naise</EmployeeName>
<EmployeeAge>22</EmployeeAge>
<EmployeeCode>1003</EmployeeCode>
<EmployeeActivity>program</EmployeeActivity>
------------------------------------------------------------------------
<EmployeeID>2</EmployeeID>
<EmployeeName>Ben Ten</EmployeeName>
<EmployeeAge>22</EmployeeAge>
<EmployeeCode>1003</EmployeeCode>
<EmployeeActivity>Eat</EmployeeActivity>
------------------------------------------------------------------------
期望的输出
<?xml version="1.0" encoding="UTF-8"?>
<EmployeeID>1</EmployeeID>
<EmployeeName>Vcut Piatos</EmployeeName>
<EmployeeAge>0</EmployeeAge>
<EmployeeCode>1002</EmployeeCode>
<EmployeeActivity>dota</EmployeeActivity>
<EmployeeActivity>program</EmployeeActivity>
------------------------------------------------------------------------
<EmployeeID>2</EmployeeID>
<EmployeeName>Ben Ten</EmployeeName>
<EmployeeAge>22</EmployeeAge>
<EmployeeCode>1003</EmployeeCode>
<EmployeeActivity>Eat</EmployeeActivity>
------------------------------------------------------------------------
答案 0 :(得分:1)
您需要按data
对id
进行分组,id
进行id
分组 - 您需要在row
的上下文中执行此操作,而不是data
的背景:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/row">
<xsl:for-each-group select="data" group-by="id">
<EmployeeID>
<xsl:value-of select="current-grouping-key()"/>
</EmployeeID>
<EmployeeName>
<xsl:value-of select='concat(fname," ",lname)' />
</EmployeeName>
<EmployeeAge>
<xsl:value-of select='age'/>
</EmployeeAge>
<EmployeeCode>
<xsl:value-of select="code"/>
</EmployeeCode>
<EmployeeActivity>
<xsl:value-of select="hobby"/>
</EmployeeActivity>
------------------------------------------------------------------------
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
注意强>:
data
,而不是program
。