如何使用XML和XSL创建锚链接?

问题描述

| 大家好,我想创建一个动物园的迷你时间表。 下面是该功能的ms paint模型。我希望看到的是从上到下的时间列表,当您单击一个列表时,页面将向下移动到该时隙并显示其详细信息。 就像HTML中的定位标记一样,我的数据也存储在XML中。 所以这是我的xml数据:
<zoo>
<animal name=\"Lion\">
<Feeding-time>11:00</Feeding-time>
</animal>
<animal name=\"Penguin\">
<Feeding-time>14:00</Feeding-time>
</animal>
<animal name=\"Elephant\">
<Feeding-time>9:00</Feeding-time>
</animal>
<animal name=\"Tortoise\">
<Feeding-time>11:00</Feeding-time>
</animal>
<animal name=\"Ape\">
<Feeding-time>16:00</Feeding-time>
</animal>
<animal name=\"Hippo\">
<Feeding-time>14:00</Feeding-time>
</animal>
<animal name=\"Rattle Snake\">
<Feeding-time>9:00</Feeding-time>
</animal>
<animal name=\"Flamingo\">
<Feeding-time>15:00</Feeding-time>
</animal>
</zoo>
我的XSL页面非常平淡:
<xsl:template match=\"/\">
<html>
<head>
  <title>Real Estate Listings</title>
  <link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\" />
</head>
   <body>

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

</xsl:stylesheet>
我知道我需要使用通过轴和/或Meunchian分组的Locator路径-我已经研究了几个小时,但我仍然不知道发生了什么。 我知道我需要使用generate-id函数,并且可以很好地使用key函数,但是同样,我也不知道如何实现它-iv在Google上花费了数小时试图弄清楚这个问题。 任何帮助都会好起来的。     

解决方法

此转换:
<xsl:stylesheet version=\"1.0\"
 xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">
 <xsl:output omit-xml-declaration=\"yes\" indent=\"yes\"/>

 <xsl:key name=\"kTimeByVal\" match=\"feeding-time\"
  use=\".\"/>

 <xsl:key name=\"kAnimalByTime\" match=\"@name\"
  use=\"../feeding-time\"/>

 <xsl:template match=\"/\">
  <xsl:apply-templates/>

  <xsl:apply-templates mode=\"group\"/>
 </xsl:template>

 <xsl:template match=
  \"feeding-time[generate-id()
               =
                generate-id(key(\'kTimeByVal\',.)[1])
               ]
  \">

  <a href=\"#{generate-id()}\">
  <xsl:value-of select=\".\"/>
  </a>
  <xsl:text> </xsl:text>
 </xsl:template>

 <xsl:template mode=\"group\" match=
  \"feeding-time[generate-id()
               =
                generate-id(key(\'kTimeByVal\',.)[1])
               ]
  \">

  <br /><p id=\"{generate-id()}\"><xsl:text/>

  <b><xsl:value-of select=\".\"/> Feeding Time for:</b></p>

  <xsl:apply-templates select=\"key(\'kAnimalByTime\',.)\"/>
 </xsl:template>

 <xsl:template match=\"@name\">
  <br /><xsl:value-of select=\".\"/>
 </xsl:template>

 <xsl:template match=\"text()\"/>
 <xsl:template mode=\"group\" match=\"text()\"/>
</xsl:stylesheet>
当应用于提供的XML文档时:
<zoo>
    <animal name=\"Lion\">
        <feeding-time>11:00</feeding-time>
    </animal>
    <animal name=\"Penguin\">
        <feeding-time>14:00</feeding-time>
    </animal>
    <animal name=\"Elephant\">
        <feeding-time>9:00</feeding-time>
    </animal>
    <animal name=\"Tortoise\">
        <feeding-time>11:00</feeding-time>
    </animal>
    <animal name=\"Ape\">
        <feeding-time>16:00</feeding-time>
    </animal>
    <animal name=\"Hippo\">
        <feeding-time>14:00</feeding-time>
    </animal>
    <animal name=\"Rattle Snake\">
        <feeding-time>9:00</feeding-time>
    </animal>
    <animal name=\"Flamingo\">
        <feeding-time>15:00</feeding-time>
    </animal>
</zoo>
产生所需的结果:
<a href=\"#d0e5\">11:00</a> 
<a href=\"#d0e11\">14:00</a> 
<a href=\"#d0e17\">9:00</a> 
<a href=\"#d0e29\">16:00</a> 
<a href=\"#d0e47\">15:00</a> 
<br/>
<p id=\"d0e5\">
   <b>11:00 Feeding Time for:</b>
</p>
<br/>Lion<br/>Tortoise<br/>
<p id=\"d0e11\">
   <b>14:00 Feeding Time for:</b>
</p>
<br/>Penguin<br/>Hippo<br/>
<p id=\"d0e17\">
   <b>9:00 Feeding Time for:</b>
</p>
<br/>Elephant<br/>Rattle Snake<br/>
<p id=\"d0e29\">
   <b>16:00 Feeding Time for:</b>
</p>
<br/>Ape<br/>
<p id=\"d0e47\">
   <b>15:00 Feeding Time for:</b>
</p>
<br/>Flamingo
它在浏览器中完全按照需要显示,并具有所需(链接单击)行为: 11:00 14:00 9:00 16:00 15:00    11:00喂食时间: 狮子龟    14:00的喂食时间: 企鹅河马    9:00喂食时间: ElephantRattle蛇    16:00喂食时间: 猿    15:00喂食时间: 火烈鸟 说明:Muenchian分组,使用ѭ5使用键来生成用作锚的唯一ID。     ,Muenchian分组使用在XSLT表的根级别定义的键,该键允许
key
函数返回满足给定条件的所有元素的列表,然后从该列表中选择第一项。例如:
<xsl:key name=\"feedingTime\" match=\"*\" use=\"feeding-time\" />
这使您可以调用
key(\'feedingTime\',\'11:00\')
以获得具有
feeding-time
元素且值
11:00
的所有元素的列表。 您可以在带有
generate-id()
函数的模板中使用此函数,该函数返回每个ID的唯一值。您可以通过将当前正在处理的元素的ID与所有具有相同“ 9”值的元素列表中的第一个元素的ID进行比较来实现此目的。像这样:
<xsl:if test=\"generate-id(feeding-time) = generate-id(key(\'feeding-time\',feeding-time)[1])\">
  <!-- generate output -->
</xsl:if>
或者,您可以在模板匹配中使用相同的条件,并使用相同的键以当前的进给时间遍历
animal
个节点的列表:
<xsl:template match=\"animal[generate-id() = generate-id(key(\'feedingTime\',feeding-time)[1])]\">
  <!-- output a heading here,current node is first animal node with each feeding-time -->
  <xsl:for-each select=\"key(\'feedingTime\',feeding-time)\">
    <!-- output each animal here -->
  </xsl:for-each>
</xsl:template>
如果这样做,则需要包含一个空模板,如下所示:
<xsl:template match=\"animal\" />
处理其余的
animal
元素,将其丢弃;您已经在上述模板的
for-each
循环中处理过它们。 您可以使用模板模式来分别处理链接和以下内容的列表,如下所示:
<xsl:key name=\"feedingTime\" match=\"*\" use=\"feeding-time\" />

<xsl:template match=\"zoo\">
  <xsl:apply-templates mode=\"links\" />
  <xsl:apply-templates mode=\"content\" />
</xsl:template>

<xsl:template match=\"animal[generate-id() = generate-id(key(\'feedingTime\',feeding-time)[1])]\" mode=\"links\">
  <a href=\"#time_{feeding-time}\">
    <xsl:value-of select=\"feeding-time\" />
  </a>
</xsl:template>

<xsl:template match=\"animal[generate-id() = generate-id(key(\'feedingTime\',feeding-time)[1])]\" mode=\"content\">
  <p id=\"time_{feeding-time}\">
    <xsl:value-of select=\"concat(feeding-time,\' Feeding time for:\')\" />
  </p>
  <ul>
    <xsl:for-each select=\"key(\'feedingTime\',feeding-time)\">
      <li>
        <xsl:value-of select=\"@name\" />
      </li>
    </xsl:for-each>
  </ul>
</xsl:template>

<xsl:template match=\"animal\" mode=\"links\" />
<xsl:template match=\"animal\" mode=\"content\" />
底部的两个模板用于处理所有先前模板未处理的
animal
元素(即,每个进给时间的第二个及后续的
animal
元素)。我没有在这里处理格式,但是希望它可以演示对您有帮助的技术。