Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables 如何在xslt中每个节点的表达式中显示在重复节点外出现一次的内容_Variables_Xslt - Fatal编程技术网

Variables 如何在xslt中每个节点的表达式中显示在重复节点外出现一次的内容

Variables 如何在xslt中每个节点的表达式中显示在重复节点外出现一次的内容,variables,xslt,Variables,Xslt,我有XML格式的数据,其中包含一些标题信息,然后是一系列项。我正在使用XSLT将其转换为另一种格式,还包括一个标题区域和一系列项 然而,在翻译后的结果中,我希望仅在标题中找到的一段数据包含在项目的每个实例中,即使它只是重复相同的值。(此值可能会更改,因此无法硬编码) 样本数据(显著简化) 我的XSLT非常复杂(不幸的是,我从别人那里继承了它,所以我不确定每件事都做些什么,我在网上自学过,但有点不知所措) 大致上,关键部件如下所示: playlist_header_title=Playli

我有XML格式的数据,其中包含一些标题信息,然后是一系列项。我正在使用XSLT将其转换为另一种格式,还包括一个标题区域和一系列项

然而,在翻译后的结果中,我希望仅在标题中找到的一段数据包含在项目的每个实例中,即使它只是重复相同的值。(此值可能会更改,因此无法硬编码)

样本数据(显著简化)

我的XSLT非常复杂(不幸的是,我从别人那里继承了它,所以我不确定每件事都做些什么,我在网上自学过,但有点不知所措)

大致上,关键部件如下所示:

    playlist_header_title=Playlist One

    playlist_title=Playlist One
    video_title=Video One

    playlist_title=Playlist One
    video_title=Video Two

    playlist_title=Playlist One
    video_title=Video Three
    <xsl:template name="rss" match="/">
      <xsl:variable name="playlist_title">
        <xsl:value-of select="string(/rss/channel/title)"/>
      </xsl:variable>
      <xsl:for-each select="rss">   
        <xsl:apply-templates name="item" select="channel/items/item"/>
      </xsl:for-each>   
    </xsl:template>

然后是一个名为“项目”的巨大模板,我不在这里介绍,但基本上它会根据需要输出所有项目数据,我只是不知道如何访问“播放列表标题”

当我尝试调用时(从模板“item”内部)


它返回一个空白。我假设这是因为该变量是在for each循环之外创建的,因此它不可用。(当我在结果标题版本的for each循环之前输出数据时,它将正确显示数据,但这还不够)

我尝试在应用模板中使用with param,还尝试将其更改为在另一个同样使用with param的循环中调用模板,但它们也显示为空白

我还尝试发送一个字符串,而不是从XML中提取播放列表标题,只是为了确认我能够将任何值传递到模板中,但结果也是空白的

例如:

<xsl:for-each select="channel/items/item">  
    <xsl:call-template name="item">
        <xsl:with-param name="playlist_title">blah</xsl:with-param>
    </xsl:call-template>
</xsl:for-each> 


<xsl:template name="item">  
    <xsl:param name="playlist_title" />
            playlist_title=<xsl:value-of select="string($playlist_title)"/>
            video_title=...
    ...
    </xsl:template>
<xsl:variable name="playlist_title">
    <xsl:value-of select="string(/rss/channel/title)"/>
</xsl:variable>
<xsl:template name="rss" match="/">
    <xsl:for-each select="rss">   
        <xsl:apply-templates name="item" select="channel/items/item"/>
    </xsl:for-each>   
</xsl:template>

废话
播放列表标题=
视频标题=。。。
...
这并没有返回值“blah”,只是一个空白。(我的希望是用从XML中提取的playlist_title值替换“blah”,但没有一个能够通过)


我被难住了!感谢您的帮助。

在您的第一个示例中,尝试在模板中引用
$playlist\u title
失败,因为该模板中不存在该变量。它是在
rss
的模板匹配中创建的,并且是本地范围的

如果您希望能够在其他模板中使用
$playlist\u title
,则需要在“全局范围”中的
rss
模板之外声明它

例如:

<xsl:for-each select="channel/items/item">  
    <xsl:call-template name="item">
        <xsl:with-param name="playlist_title">blah</xsl:with-param>
    </xsl:call-template>
</xsl:for-each> 


<xsl:template name="item">  
    <xsl:param name="playlist_title" />
            playlist_title=<xsl:value-of select="string($playlist_title)"/>
            video_title=...
    ...
    </xsl:template>
<xsl:variable name="playlist_title">
    <xsl:value-of select="string(/rss/channel/title)"/>
</xsl:variable>
<xsl:template name="rss" match="/">
    <xsl:for-each select="rss">   
        <xsl:apply-templates name="item" select="channel/items/item"/>
    </xsl:for-each>   
</xsl:template>
或者您可以删除
xsl:variable
并使用
xsl:param

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates select="rss/channel"/>         
    </xsl:template>

    <xsl:template match="channel">
        <xsl:text>playlist_header_title=</xsl:text>
        <xsl:value-of select="title"/>
        <xsl:text>&#xA;&#xA;</xsl:text>
        <xsl:apply-templates select="items/item">
            <xsl:with-param name="playlist_title" select="title"/>
        </xsl:apply-templates> 
    </xsl:template>

    <xsl:template match="item">
        <xsl:param name="playlist_title" />
        <xsl:text>playlist_title=</xsl:text>
        <xsl:value-of select="$playlist_title"/>
        <xsl:text>&#xA;</xsl:text>
        <xsl:text>video_title=</xsl:text>
        <xsl:value-of select="title"/>
        <xsl:text>&#xA;&#xA;</xsl:text>
    </xsl:template>

</xsl:stylesheet>

播放列表\标题\标题=


;
播放列表标题=

;
视频标题=


;
试试
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates select="rss/channel"/>         
    </xsl:template>

    <xsl:template match="channel">
        <xsl:text>playlist_header_title=</xsl:text>
        <xsl:value-of select="title"/>
        <xsl:text>&#xA;&#xA;</xsl:text>
        <xsl:apply-templates select="items/item">
            <xsl:with-param name="playlist_title" select="title"/>
        </xsl:apply-templates> 
    </xsl:template>

    <xsl:template match="item">
        <xsl:param name="playlist_title" />
        <xsl:text>playlist_title=</xsl:text>
        <xsl:value-of select="$playlist_title"/>
        <xsl:text>&#xA;</xsl:text>
        <xsl:text>video_title=</xsl:text>
        <xsl:value-of select="title"/>
        <xsl:text>&#xA;&#xA;</xsl:text>
    </xsl:template>

</xsl:stylesheet>