【问题标题】:Linking to an item in another node (XSLT)链接到另一个节点中的项目 (XSLT)
【发布时间】:2010-05-11 16:07:51
【问题描述】:

我有一个 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 的新手,所以请尽可能解释解决方案!提前致谢!

【问题讨论】:

  • 好问题 (+1)。请参阅我的答案以获得完整的解决方案。 :)

标签: html xml xslt


【解决方案1】:

根据您对 Dimitre 的 cmets,我认为您想要做的是使用 document() 函数访问您的“主列表”XML 文件。

您实际运行样式表的是各个片段(dano.xml、mike.xml、steve.xml),对吧?

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

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

ma​​ster_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>

史蒂夫.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>

迈克的 HTML (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>

【讨论】:

  • DevNull,看起来这就是我要找的东西,但是当我尝试运行它时出现此错误:加载样式表时出错:XSLT/XPath 函数无效。
  • 您使用的是什么处理器?我正在使用 Saxon 9,一切正常。如果您使用的是 Xalan,请尝试删除所有 data() 函数。示例:data(compName) -> compName(共有 4 个 data() 函数)
  • 抱歉,我没有使用 Saxon 或 Xalan 等 XSLT 处理器。我什至不知道从哪里开始与这些合作。我只是将其编码并在我的浏览器中预览
  • 您是否尝试删除 data() 函数?
  • 我删除了调用 data() 的变量,并更改了 value-of 以匹配此格式: ...所以现在它不再给出错误了。但它似乎仍然无法正确显示。无论我在 3 个页面中的哪一个(mike、dano、steve),Back 总是显示“dano.xml”,下一个总是“mike.xml”。
【解决方案2】:

这种转变

<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>

使用“Back”和“Next”链接生成所需的 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>

【讨论】:

  • 感谢你们的帮助。 Dimitre,您的解决方案当然有效,但我正在寻找的略有不同(抱歉,当我对此很陌生时,很难具体说明。)我目前有这些公司在名单上,每家公司都有自己的包含详细信息的 XML 文件(mike.xml、dano.xml)。后页和下一页位于每个相应的个人页面上,而不是带有主列表的页面。我怎样才能做到这一点?我是否必须将主列表放在每个公司的 xml 文件中?或者我可以使用某种变量来引用?提前致谢。
  • @Andrew-Parisi:您必须决定某些特定的 URL 方案,以便可以从其company 元素中可用的数据组成返回页面和下一页的 URL。该方案将用于生成 Back 和 Next 链接。如果您在问题中定义此 URL 方案,则可能提供一种能够准确生成所需 URL 的解决方案。
猜你喜欢
  • 1970-01-01
  • 2016-12-30
  • 1970-01-01
  • 2019-11-06
  • 2012-12-25
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多