【问题标题】:how to call one match inside another match in xsl file如何在 xsl 文件中的另一个匹配中调用一个匹配
【发布时间】:2021-10-26 22:40:49
【问题描述】:

输入:

firstdto:

  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

注意:我只想在firstdto 中导入seconddto 函数。因为我想在第一个属性中使用seconddto 属性。但我无法做到这一点。即使我将模板与seconddto 匹配,它仍然可以从firstdto 获取链接。

有人可以帮我解决这个问题吗?这对我真的很有帮助。提前致谢。

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

【问题讨论】:

  • 此问题不包含minimal reproducible example。您没有提供任何 XML。请阅读How to ask。同样xsl:template 不能是xsl:template 的后代。
  • @sspsujit 您是否将自己的代码添加到 OP 的问题中?
  • 是的@michael.hor257k
  • 我只是想知道如何在一个 xsl 中分别使用嵌套模板匹配或两个模板匹配。两种方法都适合我@michael.hor257k
  • 模板不能嵌套。请编辑您的问题并提供可重现的示例。

标签: xml xslt xslt-1.0 xslt-2.0


【解决方案1】:

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

如果你想在firstdto 中使用来自seconddto 的值,那么你可以试试这个:

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

【讨论】:

    猜你喜欢
    • 2021-05-13
    • 1970-01-01
    • 2017-05-07
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    相关资源
    最近更新 更多