1

我正在学习 XML 和 XSLT,我有两部分代码,一个是 XML,另一个是 XSLT,

这是我的 XML 文件

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="xslt.xslt"?>
<groups>
    <elements>
        <name>//1</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-03-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//2</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-03-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//3</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-07-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//4</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2009-01-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//5</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2009-02-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//6</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2008-06-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//7</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-01-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//8</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-05-14</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//9</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-05-28</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//10</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-02-25</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//11</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-12-20</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//12</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-11-07</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//13</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-07-09</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//14</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2005-10-19</PostDate>
        <trackno></trackno>
    </elements>
</groups>

这是我的 XSLT 代码

<?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>
    <h2>Try to display only 9 or 12</h2>
      <xsl:for-each select="/groups/elements">
       <xsl:sort select="PostDate" order="descending"/>
        <b><xsl:value-of select="PostDate" /></b> - 
        <font color='red'><xsl:value-of select="name" /></font>

        <p></p>
      </xsl:for-each>

  </body>
  </html>
</xsl:template>

</xsl:stylesheet> 

我的问题是如何将项目编号限制为仅显示 12 个并使它们显示在页面上 3 个项目或每行 4 个项目?

我发现有人使用位置,只是不知道要达到哪个位置来限制数字并制作行?

您可以在此处复制并粘贴我的代码以查看输出http://markbucayan.appspot.com/xslt/index.html

非常感谢任何建议和解决方案

4

4 回答 4

1

已经解决了数量限制问题,添加了大于或小于标记,仍然不知道如何使行只显示3项或一行4次

<?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>
        <h2>Try to display only 9 or 12</h2>
          <xsl:for-each select="/groups/elements">
            <xsl:sort select="PostDate" order="descending"/>
            <xsl:if test="not(position() > 10)">
            <b><xsl:value-of select="PostDate" /></b> - 
            <font color='red'><xsl:value-of select="name" /></font>
           <p></p>
           </xsl:if>
          </xsl:for-each>

      </body>
      </html>
    </xsl:template>

    </xsl:stylesheet> 
于 2012-09-06T08:37:11.757 回答
1

这种转变

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:variable name="vrtfPass1">
      <xsl:for-each select="/*/elements">
        <xsl:sort select="PostDate" order="descending"/>

        <xsl:if test="not(position() > 10)">
          <item>
            <xsl:copy-of select="PostDate|name"/>
          </item>
        </xsl:if>
      </xsl:for-each>
  </xsl:variable>

   <xsl:template match="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
   </xsl:template>

    <xsl:template match="/*">
      <table border="1">
        <xsl:apply-templates mode="pass2" select="ext:node-set($vrtfPass1)/*"/>
      </table>
    </xsl:template>

    <xsl:template mode="pass2" match="item[position() mod 3 =1]">
      <tr>
        <xsl:apply-templates mode="inRow" select=
        ".|following-sibling::*[not(position() >2)]"/>
      </tr>
    </xsl:template>

    <xsl:template match="item" mode="inRow">
      <td>
       <b><xsl:value-of select="PostDate" /></b> -
       <font color='red'><xsl:value-of select="name" /></font>
      </td>
    </xsl:template>

    <xsl:template match="item" mode="pass2"/>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<groups>
    <elements>
        <name>//1</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-03-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//2</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-03-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//3</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-07-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//4</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2009-01-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//5</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2009-02-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//6</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2008-06-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//7</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-01-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//8</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-05-14</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//9</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-05-28</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//10</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-02-25</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//11</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-12-20</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//12</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-11-07</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//13</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-07-09</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//14</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2005-10-19</PostDate>
        <trackno></trackno>
    </elements>
</groups>

产生想要的(10 个最新项目,每行 3 个项目),正确的结果:

<table border="1">
   <tr>
      <td>
         <b>2012-07-04</b> -
       <font color="red">//3</font>
      </td>
      <td>
         <b>2012-05-28</b> -
       <font color="red">//9</font>
      </td>
      <td>
         <b>2012-05-14</b> -
       <font color="red">//8</font>
      </td>
   </tr>
   <tr>
      <td>
         <b>2012-03-04</b> -
       <font color="red">//1</font>
      </td>
      <td>
         <b>2012-01-04</b> -
       <font color="red">//7</font>
      </td>
      <td>
         <b>2011-12-20</b> -
       <font color="red">//11</font>
      </td>
   </tr>
   <tr>
      <td>
         <b>2011-11-07</b> -
       <font color="red">//12</font>
      </td>
      <td>
         <b>2011-07-09</b> -
       <font color="red">//13</font>
      </td>
      <td>
         <b>2011-03-04</b> -
       <font color="red">//2</font>
      </td>
   </tr>
   <tr>
      <td>
         <b>2011-02-25</b> -
       <font color="red">//10</font>
      </td>
   </tr>
</table>
于 2012-09-06T13:09:26.170 回答
0

请检查一下,我希望对你有用

<table>
<tr>
<td>
<xsl:if test="(position() mod $pagesize) = 0 and position() != 1">
</td><td>
</xsl:if>
</td>
<tr>
</table>
于 2012-09-06T09:43:44.013 回答
0

试试这个

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="html" version="1.0" encoding="utf-8" indent="yes"  omit-xml-declaration="no"/>

  <xsl:param name="pagesize"/>

  <xsl:template match="/">
    <table>
      <tr>
        <xsl:apply-templates select="//groups/elements"/>
      </tr>
    </table>
  </xsl:template>

  <xsl:template match="//groups/elements">

    <td>
      <xsl:value-of select="name"/>
      <xsl:if test="(position() mod 3) = 0 and position() != 1">
        <xsl:text>&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;</xsl:text>
      </xsl:if>
    </td>

  </xsl:template>
</xsl:stylesheet>
于 2012-09-06T13:03:30.550 回答