English 中文(简体)
XSLT选择同一外地名称的乘数价值,并显示
原标题:XSLT select multiply value of same field name in the same node and display
  • 时间:2011-08-11 21:30:32
  •  标签:
  • xml
  • xslt

I have the following XML 法典 and I try to display by using XSLT. How to select the fieldname and return all value in a table. I tried but it won t work, just only return first record ( username and user ). Please help, many thanks.

XML 法典

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="xml_list_new.xsl"?>
<root>
   <record>
     <fieldname>username</fieldname>
        <value>user</value>
     <fieldname>ipaddress</fieldname>
        <value>127.0.0.1</value>
     <fieldname>lastaction</fieldname>
        <value>2011-06-13 00:53:05</value>
     <fieldname>sessionid</fieldname>
        <value>h0atrutpu5vgrvuf3ipledfbvl1</value>
    </record>
    <record>
     <fieldname>username</fieldname>
        <value>admin</value>
     <fieldname>ipaddress</fieldname>
        <value>127.0.0.1</value>
     <fieldname>lastaction</fieldname>
        <value>2011-06-12 00:43:53</value>
     <fieldname>sessionid</fieldname>
        <value>nnbdofsdq4sd7e3def3hnid9</value>
    </record>
    <record>
     <fieldname>username</fieldname>
         <value>ccm</value>
     <fieldname>ipaddress</fieldname>
        <value>127.0.0.1</value>
     <fieldname>lastaction</fieldname>
        <value>2011-06-10 00:30:22</value>
     <fieldname>sessionid</fieldname>
        <value>di34bndffgrt48cmkl08d8e4</value>
    </record>
 </root>

XSLT 代码

<?xml version= 1.0 ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method= html />
<xsl:param name="title">XML List</xsl:param>
<xsl:template match="/">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title><xsl:value-of select="$title"/></title>
    <style type="text/css">
        <![CDATA[

          caption { font-weight: bold; }
          th {
          background: #2C3033;
          color: #fff;
          font-family: arial;
          font-weight: bold;
          font-size: 12px;
          }
          tr {
            font-family: arial;
            color: #000;
            font-size: 12px;
            background: #E0E0E0;
          }

        ]]>
    </style>
</head>
<body>
    <div class="center" style="width: 800px; margin: 0 auto">
    <table border="0" style="width: 800px">
        <caption><xsl:value-of select="$title"/></caption>
        <thead>
            <tr>
              <xsl:for-each select="root/record">
                <th >
                <xsl:value-of select="fieldname"/>
                </th>
            </xsl:for-each>
            </tr>
        </thead>

        <tbody>
        <xsl:for-each select="root/record">
        <tr>
            <td ><xsl:value-of select="value"/></td>
        </tr>
        </xsl:for-each>
        </tbody>
    </table>
    </div>
</body>
</html>
</xsl:template> 
</xsl:stylesheet>
问题回答

看看这一模板。 我尽量遵循你的模板。 我只增加了一个模板(并清理了可读性的产出)。

[XSTL 1.0]

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>

    <xsl:template match="/">
        <table>
            <thead>
                <tr>
                    <xsl:for-each select="root/record[1]/fieldname">
                        <th>
                            <xsl:value-of select="."/>
                        </th>
                    </xsl:for-each>
                </tr>
            </thead>
            <tbody>
                <xsl:for-each select="root/record">
                    <tr>
                        <xsl:apply-templates select="value"/>
                    </tr>
                </xsl:for-each>
            </tbody>    
        </table>
    </xsl:template>

    <xsl:template match="value">
        <td>
            <xsl:value-of select="."/>
        </td>
    </xsl:template>

</xsl:stylesheet>

编制如下表格:

<table>
   <thead>
      <tr>
         <th>username</th>
         <th>ipaddress</th>
         <th>lastaction</th>
         <th>sessionid</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td>user</td>
         <td>127.0.0.1</td>
         <td>2011-06-13 00:53:05</td>
         <td>h0atrutpu5vgrvuf3ipledfbvl1</td>
      </tr>
      <tr>
         <td>admin</td>
         <td>127.0.0.1</td>
         <td>2011-06-12 00:43:53</td>
         <td>nnbdofsdq4sd7e3def3hnid9</td>
      </tr>
      <tr>
         <td>ccm</td>
         <td>127.0.0.1</td>
         <td>2011-06-10 00:30:22</td>
         <td>di34bndffgrt48cmkl08d8e4</td>
      </tr>
   </tbody>
</table>




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签