链接到另一个节点中的项目(XSLT)

时间:2010-05-11 16:07:51

标签: html xml xslt

我有一份XML文档,其中列出了公司。我想创建一个包含XSLT的链接,其中包含下一个节点的<link>子节点。很抱歉,如果这是令人困惑的..有一些我想要获得的样本XML:

<portfolio>

<company>
<name>Dano Industries</name>
<link>dano.xml</link>
</company>

<company>
<name>Mike and Co.</name>
<link>mike.xml</link>
</company>

<company>
<name>Steve Inc.</name>
<link>steve.xml</link>
</company>

</portfolio>

我想要两个链接,“BACK”和“NEXT”。虽然目前在mike.xml上,我希望BACK链接到“dano.xml”和NEXT链接到“steve.xml”......等等。并且当它在基于它周围的节点的不同页面上时动态地改变它。我想这样做是因为我可以随着时间的推移添加和更改列表,所以我不想手动重新链接所有内容。

我怎样才能获得这个?对不起,我是XSLT的新手,所以请尽可能解释一下解决方案!提前谢谢!

2 个答案:

答案 0 :(得分:1)

此转化

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

 <xsl:template match="/*">
  <html>
    <table border="1">
      <xsl:apply-templates/>
    </table>
  </html>
 </xsl:template>

 <xsl:template match="company">
   <xsl:variable name="vPrevious"
     select="preceding-sibling::company[1]/link"/>

   <xsl:variable name="vNext"
     select="following-sibling::company[1]/link"/>
   <tr>
     <td>
       <a href="{link}"><xsl:value-of select="name"/></a>
     </td>
     <td>
      &#xA0;
      <xsl:if test="$vPrevious">
       <a href="{$vPrevious}">Back</a>
      </xsl:if>
     </td>
     <td>
      &#xA0;
      <xsl:if test="$vNext">
       <a href="{$vNext}">Next</a>
      </xsl:if>
     </td>
   </tr>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档

<portfolio>
    <company>
        <name>Dano Industries</name>
        <link>dano.xml</link>
    </company>
    <company>
        <name>Mike and Co.</name>
        <link>mike.xml</link>
    </company>
    <company>
        <name>Steve Inc.</name>
        <link>steve.xml</link>
    </company>
</portfolio>

使用“后退”和“下一步”链接生成所需的HTML表格

<html>
    <table border="1">
        <tr>
            <td>
                <a href="dano.xml">Dano Industries</a>
            </td>
            <td>      </td>
            <td>
                <a href="mike.xml">Next</a>
            </td>
        </tr>
        <tr>
            <td>
                <a href="mike.xml">Mike and Co.</a>
            </td>
            <td>
                <a href="dano.xml">Back</a>
            </td>
            <td>
                <a href="steve.xml">Next</a>
            </td>
        </tr>
        <tr>
            <td>
                <a href="steve.xml">Steve Inc.</a>
            </td>
            <td>
                <a href="mike.xml">Back</a>
            </td>
            <td>      </td>
        </tr>
    </table>
</html>

答案 1 :(得分:1)

根据您对Dimitre的评论,我认为您要做的是使用document()功能访问“主列表”XML文件。

实际运行样式表的是单个片段(dano.xml,mike.xml,steve.xml),对吗?

我将使用“mike.xml”作为示例。我不知道片段是什么样的,所以我不得不做一个。您需要能够根据片段中的内容在主列表中识别正确的<company>。在我的示例中,片段具有<compName>元素,其值与主列表XML中相应公司中的<name>元素相同。

以下是“主列表”XML,“dano / mike / steve”XML,样式表以及生成的HTML的样子:

<强> master_list.xml:

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

   <company>
      <name>Dano Industries</name>
      <link>dano.xml</link>
   </company>

   <company>
      <name>Mike and Co.</name>
      <link>mike.xml</link>
   </company>

   <company>
      <name>Steve Inc.</name>
      <link>steve.xml</link>
   </company>

</portfolio>

<强> dano.xml

<?xml version="1.0" encoding="UTF-8"?>
<fragment>
   <compName>Dano Industries</compName>
   <compInfo>Some info about Dano Industries</compInfo>
</fragment>

<强> mike.xml:

<?xml version="1.0" encoding="UTF-8"?>
<fragment>
   <compName>Mike and Co.</compName>
   <compInfo>Some info about Mike and Co.</compInfo>
</fragment>

<强> steve.xml

<?xml version="1.0" encoding="UTF-8"?>
<fragment>
   <compName>Steve Inc.</compName>
   <compInfo>Some info about Steve Inc.</compInfo>
</fragment>

<强>样式表:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html" indent="yes"/>
   <xsl:strip-space elements="*"/>

   <xsl:template match="fragment">
      <xsl:variable name="name" select="compName"/>
      <xsl:variable name="previous-file">
         <xsl:value-of select="document('master_list.xml')/portfolio/company[name=$name]/preceding-sibling::company[1]/link"/>
      </xsl:variable>
      <xsl:variable name="next-file">
         <xsl:value-of select="document('master_list.xml')/portfolio/company[name=$name]/following-sibling::company[1]/link"/>
      </xsl:variable>
      <html>
         <xsl:apply-templates/>
         <p>
            <xsl:if test="not($previous-file='')">
               <a href="{$previous-file}">Back</a>
            </xsl:if>
            <xsl:if test="not($previous-file='') and not($next-file='')">
               <xsl:text>&#xA0;|&#xA0;</xsl:text>
            </xsl:if>
            <xsl:if test="not($next-file='')">
               <a href="{$next-file}">Next</a>  
            </xsl:if>
         </p>
      </html>
   </xsl:template>

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

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

</xsl:stylesheet>

Dano的HTML(dano.htm:)

<html>
   <h1>Dano Industries</h1>
   <p>Some info about Dano Industries</p>
   <p><a href="mike.xml">Next</a></p>
</html>

Mike for Mike(mike.htm:)

<html>
   <h1>Mike and Co.</h1>
   <p>Some info about Mike and Co.</p>
   <p><a href="dano.xml">Back</a>&nbsp;|&nbsp;<a href="steve.xml">Next</a></p>
</html>

史蒂夫的HTML(steve.htm:)

<html>
   <h1>Steve Inc.</h1>
   <p>Some info about Steve Inc.</p>
   <p><a href="mike.xml">Back</a></p>
</html>