首页 > 解决方案 > 如何在 xsl 文件中的另一个匹配中调用一个匹配

问题描述

输入 :

首先dto:

  1. 链接:google.com
  2. 名称:谷歌

次要

  1. 链接:yahoo.com
  2. 名称:雅虎
<sites>
  <firstdto>
    <link>google.com</link>
    <name>google</name>
  </firstdto>
  <seconddto>
    <link>yahoo.com</link>
    <name>yahoo</name>
  </seconddto>
</sites>

预期输出:

google.com
yahoo.com
google

<body>
 <link>google.com</link>
 <link>yahoo.com</link>
 <name>google</name>
</body>

输出电流:

google.com
google.com
google

注意:我只想seconddtofirstdto. 因为我想seconddto在第一个属性中使用属性。但我无法做到这一点。firstdto即使我将模板匹配到seconddto.

有人可以帮我弄这个吗。这对我真的很有帮助。提前致谢。

<xsl:stylesheet>
<Xsl:template match="/">
<head>
<style>
  .....
</style>
</head>
<body>
<xsl:apply-templates select="firstdto"/>
<xsl:apply-templates select="seconddto"/>
</body>
</xsl:template>

<xsl:template match="firstdto">
   <body>
     <xsl:value-of select="link"/>
       <xsl:template match="seconddto">
        <body>
          <xsl:value-of select="link"/>
        </body>
       </xsl:template>
     <xsl:value-of select="name">
   </body>
</xsl:template>

标签: xmlxsltxslt-1.0xslt-2.0

解决方案


我只想seconddto在里面导入函数firstdto。因为我想seconddto在第一个属性中使用属性。

如果你想使用seconddto里面的值firstdto,那么你可以试试这个:

<xsl:template match="/sites">
  <html>
    <head>
        <style>.....</style>
    </head>
    <body>
        <xsl:apply-templates select="firstdto"/>
    </body>
  </html>  
</xsl:template>

<xsl:template match="firstdto">
  <xsl:copy-of select="link"/>
  <xsl:copy-of select="../seconddto/link"/><!-- to use/copy value from other -->
  <xsl:copy-of select="name"/>
</xsl:template>

获得如下输出:

<html>
 <head>
  <style>.....</style>
 </head>
 <body>
  <link>google.com</link>
  <link>yahoo.com</link>
  <name>google</name>
 </body>
</html>

推荐阅读