1)如何添加循环,动态确定td的和
2) @ key = 'ip‘-还需要进行动态处理,从构造的t中获取一个值
<tr>
<th>Name</th>
<th>Type</th>
<th>Text</th>
<xsl:for-each-group select="//folder/element/property" group-by="@key">
<th><xsl:value-of select="current-grouping-key()"/></th>
</xsl:for-each-group>
</tr>
<tr>
<td>
<xsl:value-of select="@name"/>
</td>
<td>
<xsl:value-of select="@*[name() = 'xsi:type'][1]"/>
</td>
<td>
<xsl:value-of select="documentation"/>
</td>
<xsl:for-each select="...">
<td>
<xsl:value-of select="string-join(property[@key='ip']/@value, ', ')"/>
</td>
</xsl:for-each>
</tr>输入xml文件
<folder name="Technology & Physical" type="technology">
<element xsi:type="archimate:Node" name="SMX_U_TEST">
<documentation>SMX</documentation>
<property key="ip" value="10.255.2.111"/>
</element>
<element xsi:type="archimate:Node" name="CBS3CVR">
<documentation>DSR3CVR</documentation>
<property key="ip" value="10.15.114.24"/>
<property key="port" value="1521"/>
<property key="hw"/>
</element>
<element xsi:type="archimate:Node" name="SMX">
<property key="ip" value="10.255.2.111"/>
<property key="port" value="8181"/>
<property key="port" value="8182"/>
<property key="port" value="8184"/>
</element>
<element xsi:type="archimate:Node" name="Informatica test">
<documentation>Informatica</documentation>
<property key="ip" value="10.11.30.89"/>
<property key="port" value="1521"/>
</element>
<element xsi:type="archimate:Node" name="DSR3TEST">
<documentation>DSR3TEST</documentation>
<property key="ip" value="10.255.3.133"/>
<property key="port" value="1521"/>
<property key="hw"/>
</element>
</folder>输出html文件
<html xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<body>
<h2>Technology & Physical</h2>
<table>
<tr>
<th>Name</th>
<th>Type</th>
<th>Text</th>
<th>ip</th>
<th>port</th>
<th>hw</th>
</tr>
<tr>
<td>CBS3CVR</td>
<td>archimate:Node</td>
<td>DSR3CVR</td>
<td>10.15.114.24</td>
<td>1521</td>
<td/>
</tr>
<tr>
<td>DSR3TEST</td>
<td>archimate:Node</td>
<td>DSR3TEST</td>
<td>10.255.3.133</td>
<td>1521</td>
<td/>
</tr>
<tr>
<td>Informatica test</td>
<td>archimate:Node</td>
<td>Informatica</td>
<td>10.11.30.89</td>
<td>1521</td>
<td/>
</tr>
<tr>
<td>SMX</td>
<td>archimate:Node</td>
<td/>
<td>10.255.2.111</td>
<td>8181, 8182, 8184</td>
<td/>
</tr>
<tr>
<td>SMX_U_TEST</td>
<td>archimate:Node</td>
<td>SMX</td>
<td>10.255.2.111</td>
<td/>
<td/>
</tr>
</table>
</body>
</html>元素和属性的数量是不受限制的,键的名称可以不同。我用group by,如果你有别的决定,告诉我。谢谢!
发布于 2017-08-21 18:39:53
您可以在这里使用distinct-values来获取不同的property密钥,而不是使用xsl:for-each-group
<xsl:variable name="properties" select="distinct-values(element/property/@key)" />(假设您位于folder元素上)。
然后,对于XML中的每个element,可以像这样列出属性的值。
<xsl:variable name="current" select="." />
<xsl:for-each select="$properties">
<td>
<xsl:value-of select="$current/property[@key=current()]/@value" separator=", " />
</td>
</xsl:for-each>试试这个XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="xsi">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="folder">
<xsl:variable name="properties" select="distinct-values(element/property/@key)" />
<table>
<tr>
<th>Name</th>
<th>Type</th>
<th>Text</th>
<xsl:for-each select="$properties">
<th><xsl:value-of select="."/></th>
</xsl:for-each>
</tr>
<xsl:for-each select="element">
<tr>
<td>
<xsl:value-of select="@name"/>
</td>
<td>
<xsl:value-of select="@xsi:type"/>
</td>
<td>
<xsl:value-of select="documentation"/>
</td>
<xsl:variable name="current" select="." />
<xsl:for-each select="$properties">
<td>
<xsl:value-of select="$current/property[@key=current()]/@value" separator=", " />
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>我假设您的实际XML确实声明了xsi名称空间前缀。我还在XSLT中声明了它,以便更简单地检索xsl:type属性。
编辑:如果只能使用XSLT1.0处理程序,就不能使用distinct-values获取密钥。相反,您需要使用一种名为Muenchian Grouping的技术来获取不同的键。(您也不能在xsl:value-of上使用separator属性)。
请尝试使用此XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="xsi">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:key name="properties" match="property" use="@key" />
<xsl:template match="folder">
<xsl:variable name="properties" select="//property[generate-id() = generate-id(key('properties', @key)[1])]/@key" />
<table>
<tr>
<th>Name</th>
<th>Type</th>
<th>Text</th>
<xsl:for-each select="$properties">
<th><xsl:value-of select="."/></th>
</xsl:for-each>
</tr>
<xsl:for-each select="element">
<tr>
<td>
<xsl:value-of select="@name"/>
</td>
<td>
<xsl:value-of select="@xsi:type"/>
</td>
<td>
<xsl:value-of select="documentation"/>
</td>
<xsl:variable name="current" select="." />
<xsl:for-each select="$properties">
<td>
<xsl:for-each select="$current/property[@key=current()]/@value">
<xsl:if test="position() > 1">,</xsl:if>
<xsl:value-of select="." />
</xsl:for-each>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/45788902
复制相似问题