显示一个表,其中显示每行包含每个子节点的父节点

时间:2015-11-26 22:01:50

标签: xml xslt-1.0

我有以下XML需要以这种格式显示在表中:

第1栏:

要求

REQ-1

REQ-2

REQ-3

REQ-4

第2栏:

验收测试

AT-1

AT-1

AT-1

AT-2

如何使用XSLT1.0实现此目的?

<?xml version="1.0" encoding="UTF-8"?>

<root>
<Item type="Acceptance Test">
  <jav_acc_id>AT-1</jav_acc_id>
  <Relationships>
     <Item type="Acceptance Test Requirements">
        <related_id>
           <Item type="Requirement">
              <item_number>REQ-1</item_number>
           </Item>
        </related_id>
     </Item>
     <Item type=" Acceptance Test Requirements ">
        <related_id>
           <Item type="Requirement">
              <item_number>REQ-2</item_number>
           </Item>
        </related_id>
     </Item>
     <Item type=" Acceptance Test Requirements ">
        <related_id>
           <Item type="Requirement">
              <item_number>REQ-3</item_number>
           </Item>
        </related_id>
     </Item>
    </Relationships>
</Item>
<Item type="Acceptance Test">
  <jav_acc_id>AT-2</jav_acc_id>
  <Relationships>
     <Item type="Acceptance Test Requirements">
        <related_id>
           <Item type="Requirement">
              <item_number>REQ-4</item_number>
           </Item>
        </related_id>
     </Item>
  </Relationships>
</Item>
</root>

这就是我所拥有的(它创建了一个错误的输出,但我不知道如何解决它)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
    <html>
    <body>
        <table border="1">
              <th style="text-align:left">Requirement</th>
              <th style="text-align:left">Acceptance Test</th>
          </tr>
          <xsl:for-each select="descendant::Item">
              <tr>
                  <td><xsl:value-of select="item_number"/></td>
                  <td><xsl:value-of select="jav_acc_id"/></td>
              </tr>
            </xsl:for-each>
        </table>
    </body>
</html>
</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

鉴于以下格式良好的输入:

<强> XML

<root>
   <Item type="Acceptance Test">
      <jav_acc_id>AT-1</jav_acc_id>
      <Relationships>
         <Item type="Acceptance Test Requirements">
            <related_id>
               <Item type="Requirement">
                  <item_number>REQ-1</item_number>
               </Item>
            </related_id>
         </Item>
         <Item type=" Acceptance Test Requirements ">
            <related_id>
               <Item type="Requirement">
                  <item_number>REQ-2</item_number>
               </Item>
            </related_id>
         </Item>
         <Item type=" Acceptance Test Requirements ">
            <related_id>
               <Item type="Requirement">
                  <item_number>REQ-3</item_number>
               </Item>
            </related_id>
         </Item>
      </Relationships>
   </Item>
   <Item type="Acceptance Test">
      <jav_acc_id>AT-2</jav_acc_id>
      <Relationships>
         <Item type="Acceptance Test Requirements">
            <related_id>
               <Item type="Requirement">
                  <item_number>REQ-4</item_number>
               </Item>
            </related_id>
         </Item>
      </Relationships>
   </Item>
</root>

以下样式表:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/root">
    <table border="1">
        <tr>
            <th>Requirement</th>
            <th>Acceptance Test</th>
        </tr>   
        <xsl:for-each select="Item/Relationships/Item">
            <tr>
                <td>
                    <xsl:value-of select=".//item_number"/>
                </td>
                <td>
                    <xsl:value-of select="../../jav_acc_id"/>
                </td>
            </tr>   
        </xsl:for-each>
    </table>
</xsl:template>

</xsl:stylesheet>

将返回:

<强>结果

<table border="1">
   <tr>
      <th>Requirement</th>
      <th>Acceptance Test</th>
   </tr>
   <tr>
      <td>REQ-1</td>
      <td>AT-1</td>
   </tr>
   <tr>
      <td>REQ-2</td>
      <td>AT-1</td>
   </tr>
   <tr>
      <td>REQ-3</td>
      <td>AT-1</td>
   </tr>
   <tr>
      <td>REQ-4</td>
      <td>AT-2</td>
   </tr>
</table>

呈现为:

enter image description here