如何将数据从xml中的标签数据拉到xslt

问题描述:

需要从xml中的每组标签中提取数据。现在我只获取每行的第一组数据。这里是XML我拉数据从如何将数据从xml中的标签数据拉到xslt

<message> 
    <Data> 
     <Name>John Doe</Name> 
     <Date>2/14/2012</Date> 
     <Phone>1234567</Phone> 
    </Data> 

    <Data> 
    <Name>Jane Doe</Name> 
     <Date>4/19/2012</Date> 
     <Phone>2345678</Phone> 
    </Data> 

    <Data> 
     <Name>Mike Doe</Name> 
     <Date>12/14/2011</Date> 
     <Phone>3456789</Phone> 
    </Data> 
    </message> 

我使用的XSLT是这样的。

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > 
    <xsl:output method="html" version="1.1" encoding="iso-8859-1" /> 

    <xsl:template match="/message"> 
    <html> 
    <body> 
    <table border="1"> 
    <tr> 
      <th ColSpan="4">Person</th> 
    </tr> 
    <tr> 
      <th>Name</th> 
      <th>Date</th> 
     <th>Phone</th> 
      </tr> 
    <xsl:for-each select="//Data"> 
     <tr> 
    <td> 
    <xsl:value-of select="//Name" /> 
     </td> 
     <td> 
     <xsl:value-of select="//Date" /> 
     </td> 
     <td> 
     <xsl:value-of select="//Phone" /> 
     </td> 
     </tr> 
    </xsl:for-each> 
    </table> 
    </body> 
    </html> 
    </xsl:template> 
</xsl:stylesheet> 

我的输出结果只显示John Doe关于所有三行的信息。

尝试删除所有//的...

<xsl:for-each select="Data"> 
     <tr> 
      <td> 
       <xsl:value-of select="Name" /> 
      </td> 
      <td> 
       <xsl:value-of select="Date" /> 
      </td> 
      <td> 
       <xsl:value-of select="Phone" /> 
      </td> 
     </tr> 
    </xsl:for-each> 

<tr> 
    <td> 
     <xsl:value-of select="//Name" /> 
    </td> 
    <td> 
     <xsl:value-of select="//Date" /> 
    </td> 
    <td> 
     <xsl:value-of select="//Phone" /> 
    </td> 
</tr> 

你的问题就出在这里。您已选择所有数据标签并对它们进行迭代,但是当您获取该值时,您将获取文档中的所有名称,日期或电话标签,然后获取第一个标签的值是John Doe的。

<tr> 
    <td> 
     <xsl:value-of select="Name" /> 
    </td> 
    <td> 
     <xsl:value-of select="Date" /> 
    </td> 
    <td> 
     <xsl:value-of select="Phone" /> 
    </td> 
</tr> 

由于在for-each中,您处于Data标记的范围内,因此可以使用名称来选择子节点。

为了XSLT的风格,我还建议将它们分成一个模板。所以,你最终的变换应该是这样的:

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > 
    <xsl:output method="html" version="1.1" encoding="iso-8859-1" /> 
    <xsl:template match="/message"> 
     <html> 
      <body> 
       <table border="1"> 
        <tr> 
         <th ColSpan="4">Person</th> 
        </tr> 
        <tr> 
         <th>Name</th> 
         <th>Date</th> 
         <th>Phone</th> 
        </tr> 
        <xsl:apply-templates select="Data"/> 
       </table> 
      </body> 
     </html> 
    </xsl:template> 
    <xsl:template match="Data"> 
     <tr> 
      <td> 
       <xsl:value-of select="Name" /> 
      </td> 
      <td> 
       <xsl:value-of select="Date" /> 
      </td> 
      <td> 
       <xsl:value-of select="Phone" /> 
      </td> 
     </tr> 
    </xsl:template> 
</xsl:stylesheet> 

例子:http://www.xsltcake.com/slices/MzgTXl