我有这样的 XML
<root>
<Users groupid="1">
<User id="person1" name="ABC" parentid="person7"/>
<User id="person2" name="xyz" parentid="person1"/>
<User id="person3" name="LMN" parentid="bac3"/>
<User id="person4" name="PQR" parentid="person2"/>
<User id="person5" name="PQR" parentid="person1"/>
<User id="person6" name="PQR" parentid="person7"/>
</Users>
<Users groupid="3">
<User id="person7" name="ABC" parentid="person11"/>
<User id="person8" name="xyz" parentid="person1"/>
<User id="person3" name="LMN" parentid="bac3"/>
<User id="person4" name="PQR" parentid="person2"/>
<User id="person5" name="PQR" parentid="person1"/>
<User id="person6" name="PQR" parentid="person7"/>
</Users>
</root>
我需要使用xslt 1.0将其转换为=>
<Users groupid="1">
<User id="person1" name="ABC" parentid="person7" haschildinGroup="yes"/>
<User id="person2" name="xyz" parentid="person1" haschildinGroup="yes"/>
<User id="person4" name="PQR" parentid="person2" haschildinGroup="no"/>
<User id="person5" name="PQR" parentid="person1" haschildinGroup="no"/>
<User id="person3" name="LMN" parentid="bac3" haschildinGroup="no"/>
<User id="person6" name="PQR" parentid="person7" haschildinGroup="no"/>
</Users>
<Users groupid="3">
<User id="person7" name="ABC" parentid="person11" haschildinGroup="yes"/>
<User id="person6" name="PQR" parentid="person7" haschildinGroup="no"/>
<User id="person8" name="xyz" parentid="person1" haschildinGroup="no"/>
<User id="person3" name="LMN" parentid="bac3" haschildinGroup="yes"/>
<User id="person5" name="PQR" parentid="person3" haschildinGroup="no"/>
<User id="person4" name="PQR" parentid="person2" haschildinGroup="no"/>
</Users>
指南: 1)我需要根据共享 parentid 及以上的人对值进行分组输出 xml 解释更多
2) 需要将 xml 缩进类似父子关系的缩进(在第一个添加额外的空格)用于 HTML 视图
我尝试过这样的示例,但其中的关键在整个 XML 上而不是在当前节点内
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="no"/>
<xsl:key name="groups" match="User" use="@parentid" />
<xsl:variable name="Space">&nbsp;</xsl:variable>
<xsl:variable name="addSpace" select="concat($Space,$Space)"/>
<xsl:template match="/">
<html>
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<xsl:for-each select="root/Users/User[generate-id(.)=generate-id(key('groups',@parentid)[1])]">
<xsl:call-template name="selectusers"></xsl:call-template>
</xsl:for-each>
</table>
</html>
</xsl:template>
<xsl:template name="selectusers">
<xsl:for-each select="key('groups', @parentid)">
<tr>
<xsl:value-of select="$addSpace" disable-output-escaping="yes"/><!--Works for only 1st Level need to add If-->
<td>
<xsl:value-of select="@id"/>
</td>
<td>
<!--HasChild =:(-->
</td>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>