使用XSLT将xml转换为表

时间:2022-01-20 21:50:33

I have this xml

我有这个xml

<?xml version='1.0' encoding='utf-8'?>
<DATA>
  <PERSONS>
    <PERSON id="792226" member_id="53534">
      <FNAME>Alfred</FNAME>
      <MNAME> C  </MNAME>
      <LNAME> Kenneth </LNAME>
      <EMAIL> ac1@abc.com </EMAIL>
      <USER_ROLES>
        <ROLE> Abstract </ROLE_NAME>
      </USER_ROLES>
    </PERSON>
    <PERSON id="792319" member_id="34526">
      <FNAME> Adam </FNAME>
      <MNAME> F  </MNAME>
      <LNAME> Robert </LNAME>
      <EMAIL> adam@xyz.com </EMAIL>
      <USER_ROLES>
        <ROLE> General</ROLE>
        <ROLE> Reviewer </ROLE>
      </USER_ROLES>
      <AREAS_OF_EXPERTISE>
        <EXPERTISE> Dynamics</EXPERTISE>
        <EXPERTISE> Engineering </EXPERTISE>
        <EXPERTISE> Exploration</EXPERTISE>
      </AREAS_OF_EXPERTISE>
    </PERSON>
 </PERSONS>
</DATA>

would like to convert as html table using xslt. I want to pull only id, member_id, fname, Role and Expertise..Any help would be appreciated.

想使用xslt转换为html表。我想只拉取id,member_id,fname,角色和专业知识。任何帮助将不胜感激。

Updated : I have applied this xslt

更新:我已经应用了这个xslt

<xsl:template match="/">
 <html>
 <head>
 </head>
 <body>
  <table border="1">
    <tr>
        <th>ID</th>
        <th>Member ID</th>
        <th>First Name</th>
        <th>ROLE</th>
        <th>Expertise</th>

    </tr>
        <xsl:for-each select="DATA/PERSONS/PERSON">
        <tr>
                <td><xsl:value-of select="@id"/></td>
                <td><xsl:value-of select="@member_id"/></td>
                <td><xsl:value-of select="FNAME"/></td>
                <td><xsl:value-of select="USER_ROLES/ROLE"/></td>
                <td><xsl:value-of select="AREAS_OF_EXPERTISE/EXPERTISE"/></td>

        </tr>   
        </xsl:for-each>             
</table>
</body>
</html>

But I am not sure how to read roles and expertise when it is more than one node. It always shows first node only. if role or expertise are more then one then it should be in a new row with id, memberid, fname and email.

但是,当不止一个节点时,我不确定如何阅读角色和专业知识。它始终仅显示第一个节点。如果角色或专业知识超过一个,那么它应该在id,memberid,fname和email的新行中。

I would like to display as

我想显示为

使用XSLT将xml转换为表

Edited : It should display in new row for each role and expertise. So, For id 792319, it will 5 rows, 2 rows for role and 3 rows for expertise. Little weird, but that is the way they wanted. Some records may not have no roles and expertise.'

编辑:它应该显示每个角色和专业知识的新行。因此,对于id 792319,它将为5行,2行为角色,3行为专业知识。有点奇怪,但这就是他们想要的方式。有些记录可能没有任何角色和专业知识。

I tried to upload image what I want, but I dont have that much reputation to do that :)

我试图上传我想要的图像,但我没有那么多的声誉去做:)

EDIT :

编辑:

Finally, I have developed this xslt. I got idea from Michael suggestion. Hope this will help others. This xslt read all the records together.

最后,我开发了这个xslt。我从迈克尔的建议中得到了建议。希望这会有助于他人。这个xslt一起读取所有记录。

<xsl:template match="/">
 <html>
 <head>
 </head>
 <body>
   <table border="1">
    <tr>
        <th>ID</th>
        <th>Member ID</th>
        <th>First Name</th>
        <th>Email</th>
        <th>ROLE</th>
        <th>Expertise</th>

    </tr>
<xsl:for-each select="DATA/PERSONS/PERSON">

                <xsl:choose>
                  <xsl:when test="count(USER_ROLES) > 0 or count(AREAS_OF_EXPERTISE) > 0">
                  <xsl:for-each select="USER_ROLES/ROLE" >
                      <tr>
                        <td><xsl:value-of select="../../@id"/></td>
                        <td><xsl:value-of select="../../@member_id"/></td>
                        <td><xsl:value-of select="../../FNAME"/></td>
                        <td><xsl:value-of select="../../EMAIL"/></td>
                        <td><xsl:value-of select="self::ROLE"/></td>
                        <td></td>
                        </tr>
                    </xsl:for-each>
                    <xsl:for-each select="AREAS_OF_EXPERTISE/EXPERTISE" >
                      <tr>
                        <td><xsl:value-of select="../../@id"/></td>
                        <td><xsl:value-of select="../../@member_id"/></td>
                        <td><xsl:value-of select="../../FNAME"/></td>
                        <td><xsl:value-of select="../../EMAIL"/></td>
                        <td></td>
                        <td><xsl:value-of select="self::EXPERTISE"/></td>
                        </tr>
                    </xsl:for-each>
                  </xsl:when>
                            <xsl:otherwise>
                            <tr>
                                <td><xsl:value-of select="@id"/></td>
                                <td><xsl:value-of select="@member_id"/></td>
                                <td><xsl:value-of select="FNAME"/></td>
                                <td><xsl:value-of select="EMAIL"/></td>
                                <td></td>
                                <td></td>
                            </tr>
                          </xsl:otherwise>      
                 </xsl:choose>

        </xsl:for-each> 
  </table>
  </body>
  </html>
</xsl:template>         

1 个解决方案

#1


1  

Would a table like this satisfy your requirements?

这样的桌子能满足你的要求吗?

使用XSLT将xml转换为表


Edit:

It is very simple to produce a table like you want:

生成一个你想要的表非常简单:

使用XSLT将xml转换为表

XSLT 1.0

XSLT 1.0

<?xml version="1.0" encoding="utf-8"?>
<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:template match="/">
<table border="1">
    <tr>
        <th>ID</th>
        <th>Member ID</th>
        <th>First Name</th>
        <th>e-Mail</th>
        <th>Role</th>
        <th>Expertise</th>
    </tr>
    <xsl:for-each select="DATA/PERSONS/PERSON/USER_ROLES/ROLE | DATA/PERSONS/PERSON/AREAS_OF_EXPERTISE/EXPERTISE">
        <tr>
            <td><xsl:value-of select="../../@id"/></td>
            <td><xsl:value-of select="../../@member_id"/></td>
            <td><xsl:value-of select="../../FNAME"/></td>
            <td><xsl:value-of select="../../EMAIL"/></td>
            <td><xsl:value-of select="self::ROLE"/></td>
            <td><xsl:value-of select="self::EXPERTISE"/></td>
        </tr>
    </xsl:for-each>
</table>
</xsl:template>

</xsl:stylesheet>

However, if a person has neither a role nor an expertise, they will not be included.

但是,如果一个人既没有角色也没有专业知识,他们就不会被包括在内。


Edit 2:

To append the people with no roles nor expertise to the above table, just add another row for each such person. Here's the modified stylesheet:

要将没有角色或专业知识的人员附加到上表中,只需为每个此类人员添加另一行。这是修改后的样式表:

<?xml version="1.0" encoding="utf-8"?>
<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:template match="/">
<table border="1">
    <tr>
        <th>ID</th>
        <th>Member ID</th>
        <th>First Name</th>
        <th>e-Mail</th>
        <th>Role</th>
        <th>Expertise</th>
    </tr>
    <xsl:for-each select="DATA/PERSONS/PERSON/USER_ROLES/ROLE | DATA/PERSONS/PERSON/AREAS_OF_EXPERTISE/EXPERTISE">
        <tr>
            <td><xsl:value-of select="../../@id"/></td>
            <td><xsl:value-of select="../../@member_id"/></td>
            <td><xsl:value-of select="../../FNAME"/></td>
            <td><xsl:value-of select="../../EMAIL"/></td>
            <td><xsl:value-of select="self::ROLE"/></td>
            <td><xsl:value-of select="self::EXPERTISE"/></td>
        </tr>
    </xsl:for-each>
    <xsl:for-each select="DATA/PERSONS/PERSON[not(USER_ROLES) and not(AREAS_OF_EXPERTISE)]">
        <tr>
            <td><xsl:value-of select="@id"/></td>
            <td><xsl:value-of select="@member_id"/></td>
            <td><xsl:value-of select="FNAME"/></td>
            <td><xsl:value-of select="EMAIL"/></td>
            <td/>
            <td/>
        </tr>
    </xsl:for-each>
</table>
</xsl:template>

</xsl:stylesheet>

#1


1  

Would a table like this satisfy your requirements?

这样的桌子能满足你的要求吗?

使用XSLT将xml转换为表


Edit:

It is very simple to produce a table like you want:

生成一个你想要的表非常简单:

使用XSLT将xml转换为表

XSLT 1.0

XSLT 1.0

<?xml version="1.0" encoding="utf-8"?>
<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:template match="/">
<table border="1">
    <tr>
        <th>ID</th>
        <th>Member ID</th>
        <th>First Name</th>
        <th>e-Mail</th>
        <th>Role</th>
        <th>Expertise</th>
    </tr>
    <xsl:for-each select="DATA/PERSONS/PERSON/USER_ROLES/ROLE | DATA/PERSONS/PERSON/AREAS_OF_EXPERTISE/EXPERTISE">
        <tr>
            <td><xsl:value-of select="../../@id"/></td>
            <td><xsl:value-of select="../../@member_id"/></td>
            <td><xsl:value-of select="../../FNAME"/></td>
            <td><xsl:value-of select="../../EMAIL"/></td>
            <td><xsl:value-of select="self::ROLE"/></td>
            <td><xsl:value-of select="self::EXPERTISE"/></td>
        </tr>
    </xsl:for-each>
</table>
</xsl:template>

</xsl:stylesheet>

However, if a person has neither a role nor an expertise, they will not be included.

但是,如果一个人既没有角色也没有专业知识,他们就不会被包括在内。


Edit 2:

To append the people with no roles nor expertise to the above table, just add another row for each such person. Here's the modified stylesheet:

要将没有角色或专业知识的人员附加到上表中,只需为每个此类人员添加另一行。这是修改后的样式表:

<?xml version="1.0" encoding="utf-8"?>
<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:template match="/">
<table border="1">
    <tr>
        <th>ID</th>
        <th>Member ID</th>
        <th>First Name</th>
        <th>e-Mail</th>
        <th>Role</th>
        <th>Expertise</th>
    </tr>
    <xsl:for-each select="DATA/PERSONS/PERSON/USER_ROLES/ROLE | DATA/PERSONS/PERSON/AREAS_OF_EXPERTISE/EXPERTISE">
        <tr>
            <td><xsl:value-of select="../../@id"/></td>
            <td><xsl:value-of select="../../@member_id"/></td>
            <td><xsl:value-of select="../../FNAME"/></td>
            <td><xsl:value-of select="../../EMAIL"/></td>
            <td><xsl:value-of select="self::ROLE"/></td>
            <td><xsl:value-of select="self::EXPERTISE"/></td>
        </tr>
    </xsl:for-each>
    <xsl:for-each select="DATA/PERSONS/PERSON[not(USER_ROLES) and not(AREAS_OF_EXPERTISE)]">
        <tr>
            <td><xsl:value-of select="@id"/></td>
            <td><xsl:value-of select="@member_id"/></td>
            <td><xsl:value-of select="FNAME"/></td>
            <td><xsl:value-of select="EMAIL"/></td>
            <td/>
            <td/>
        </tr>
    </xsl:for-each>
</table>
</xsl:template>

</xsl:stylesheet>