我有像这样的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 转换为=&gt;
<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像缩进
我试过这样的例子但是 Key 在这个范围内有整个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>
答案 0 :(得分:1)
您的XSLT是为了生成HTML而不是您所描述的所需输出而编写的,但我会假设您想要的实际上是您尝试生成的HTML。如果是这样,应该这样做:
<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="concat(../@groupid, '+', @parentid)" />
<xsl:variable name="Space"> </xsl:variable>
<xsl:variable name="addSpace" select="concat($Space,$Space)"/>
<xsl:template match="/">
<html>
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<th>ID</th>
<th>Has Children</th>
</tr>
<xsl:apply-templates
select="root/Users/User[not(@parentid = ../User/@id)]" />
</table>
</html>
</xsl:template>
<xsl:template match="User">
<xsl:variable name="children"
select="key('groups', concat(../@groupid, '+', @id))" />
<tr>
<td>
<xsl:apply-templates select="." mode="spacing" />
<xsl:value-of select="@id"/>
</td>
<td>
<xsl:value-of select="substring('No Yes', 1 + 3 * boolean($children), 3)"/>
</td>
</tr>
<xsl:apply-templates select="$children" />
</xsl:template>
<xsl:template match="User" mode="spacing">
<xsl:value-of select="$addSpace" />
<xsl:apply-templates
select="../User[@id = current()/@parentid]" mode="spacing" />
</xsl:template>
</xsl:stylesheet>
在样本输入上运行此操作时,结果为:
<html>
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<th>ID</th>
<th>Has Children</th>
</tr>
<tr>
<td> person1</td>
<td>Yes</td>
</tr>
<tr>
<td> person2</td>
<td>Yes</td>
</tr>
<tr>
<td> person4</td>
<td>No </td>
</tr>
<tr>
<td> person5</td>
<td>No </td>
</tr>
<tr>
<td> person3</td>
<td>No </td>
</tr>
<tr>
<td> person6</td>
<td>No </td>
</tr>
<tr>
<td> person7</td>
<td>Yes</td>
</tr>
<tr>
<td> person6</td>
<td>No </td>
</tr>
<tr>
<td> person8</td>
<td>No </td>
</tr>
<tr>
<td> person3</td>
<td>No </td>
</tr>
<tr>
<td> person4</td>
<td>No </td>
</tr>
<tr>
<td> person5</td>
<td>No </td>
</tr>
</table>
</html>