<memberuser name="John">
<connection>Tim</connection>
<connection>Mary</connection>
<connection>Alen</connection>
</memberuser>
<memberuser name="Alen">
<connection>Jill</connection>
<connection>Elen</connection>
<connection>John</connection>
</memberuser>
假设我有一个像上面那样的xml。如何通过使用xslt连接我的连接来计算和关联。所以对于会员用户John来说。 Jill是Alen的连接,是连接John的连接。我想计算并为每个用户显示连接的连接。我想计算这样的东西:
<connection>Tim</connection>
<connection>Mary</connection>
<connection>Alen</connection>
<connectionofconnection>
<from>Allen</from>
<name>Jill</name>
</connectionofconnection>
答案 0 :(得分:1)
如果你想将世代数限制为两个(孩子和孙子),你可以这样做:
XSLT 1.0
已编辑以消除第二代连接列表中的自我和直接连接。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="member" match="memberuser" use="@name" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="connection">
<xsl:copy-of select="."/>
<xsl:apply-templates select="key('member', .)/connection[not(.=current()/../@name or .=current()/../connection)]" mode="gen2"/>
</xsl:template>
<xsl:template match="connection" mode="gen2">
<connection thru="{../@name}">
<xsl:value-of select="." />
</connection>
</xsl:template>
</xsl:stylesheet>
应用于以下测试输入:
<root>
<memberuser name="Adam">
<connection>Betty</connection>
<connection>Cecil</connection>
<connection>David</connection>
</memberuser>
<memberuser name="Betty">
<connection>Adam</connection>
<connection>Cecil</connection>
<connection>Eve</connection>
</memberuser>
</root>
您将收到以下输出:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<memberuser name="Adam">
<connection>Betty</connection>
<connection thru="Betty">Eve</connection>
<connection>Cecil</connection>
<connection>David</connection>
</memberuser>
<memberuser name="Betty">
<connection>Adam</connection>
<connection thru="Adam">David</connection>
<connection>Cecil</connection>
<connection>Eve</connection>
</memberuser>
</root>