1

我有xml文件:

<root>
<host>
<address addr="192.168.0.1" addrtype="ipv4"/>
<ports>
<port protocol="tcp" portid="10"></port>
<port protocol="tcp" portid="20"></port>
</ports>
</host>
<host>
<address addr="192.168.0.2" addrtype="ipv4"/>
<ports>
<port protocol="tcp" portid="30"></port>
<port protocol="tcp" portid="40"></port>
</ports>
</host>
<root>

我想得到这样的结果:

192.168.0.1 10,20
192.168.0.2 30,40

我正在使用 for-each (第一列):

<xsl:for-each select="root/host">
<xsl:value-of select="address/@addr"/></fo:block>
</xsl:for-each>

但是第二列是一个问题..我得到错误的结果:

192.168.0.1 10,20,30,40
192.168.0.2 10,20,30,40 

请帮忙 :)

4

2 回答 2

1

您需要再次遍历所有子“端口”元素。以下应该有效:

<xsl:for-each select="root/host">
   <xsl:value-of select="address/@addr" />
   <xsl:text> </xsl:text>
   <xsl:for-each select="ports/port">
      <xsl:value-of select="@portid" />
      <xsl:if test="following-sibling::*">,</xsl:if>
      <xsl:text>&#xa;</xsl:text>
   </xsl:for-each>
</xsl:for-each>
于 2013-10-23T12:09:17.003 回答
1
<xsl:for-each select="root/host">
<xsl:value-of select="address/@addr"/>
<xsl:for-each select="ports/port">
<xsl:value-of select="@portid"/>
</xsl:for-each>
</xsl:for-each>

您可以在上面尝试一些示例 xslt 编辑器,例如 http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex3

于 2013-10-23T12:15:30.140 回答