如何使用XSL转换具有多个内部循环的XML

时间:2017-06-06 11:19:17

标签: xml xslt

我有一个带有多个内部循环的XML,我需要循环遍历1个节点并在HTML视图中显示它。 XML在下面给出

 <?xml version ="1.0" encoding ="utf-8" ?>
    <TestUser>
      <Users>
        <UserData name="test123" address="USA"/>
        <UserCommunication>
          <Communication mode="Te" value="123456879"/>
          <Qualification>
            <PG value="No"></PG>
          </Qualification>
            <Qualification>
            <UG value="YES"></UG>
          </Qualification>
        </UserCommunication>
      </Users>
      <Users>
        <UserData name="test124" address="UK"/>
        <UserCommunication>
          <Communication mode="Te" value="1567894525"/>
          <Qualification>
            <PG value="No"></PG>
          </Qualification>
           <Qualification>
            <UG value="YES"></UG>
          </Qualification>
        </UserCommunication>
      </Users>
      <Users>
        <UserData name="test125" address="INDIA"/>
        <UserCommunication>
          <Communication mode="Te" value="5465897845"/>
          <Qualification>
            <PG value="YES"></PG>
          </Qualification>
           <Qualification>
            <UG value="YES"></UG>
          </Qualification>
        </UserCommunication>
      </Users>
    </TestUser>

我需要使用XSLT在HTML视图中逐个显示用户详细信息,如下图所示。 enter image description here 任何人都可以帮我实现这个目标吗?

1 个答案:

答案 0 :(得分:1)

Check following Code:-
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="TestUser">
<html>
<head>
</head>
<body>
<table border="5" style="border-collapse: collapse;">
<thead>
<th bgcolor="greeen">User Name</th>
<th bgcolor="greeen">User_Addrss</th>
<th bgcolor="greeen">User_Telephone</th>
<th bgcolor="greeen">PG</th>
<th bgcolor="greeen">UG</th>
</thead>
<xsl:for-each select="//Users/UserData">
<tr>
<td><xsl:value-of select="@name"/></td>
<td><xsl:value-of select="@address"/></td>
<td><xsl:value-of select="../UserCommunication/Communication/@value"/></td>
<td><xsl:value-of select="../UserCommunication/Qualification/PG/@value"/> </td>
<td><xsl:value-of select="../UserCommunication/Qualification/UG/@value"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>