1

我花了2个小时在互联网上寻找帮助,但我没有找到任何答案......

我希望你能:)

所以,我的 xslt 文件如下(简化):

<?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" />
    <xsl:param name="basename"/>
    <xsl:param name="purpose"/>
    <xsl:param name="xml_input_path"/>
    <xsl:param name="self"/>

    <xsl:template match="testspec">
        <xsl:call-template name="call_commands"/>
    </xsl:template>

    <xsl:template name="call_commands">
        <xsl:variable name="root" select="document($xml_input_path)/testspec"/>
        <xsl:for-each select="$root//command">
            <xsl:sort select="."/>
            <xsl:variable name="current" select="."/>
            <xsl:apply-templates select="document($self)/xsl:stylesheet/xsl:template[@name = $current/@label]"/>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="TOTO_short">
        <xsl:text>Fonction TOTO :</xsl:text>
        <xsl:variable name="dda" select="'b'"/>
        <xsl:copy-of select="$dda"/>
    </xsl:template>

    <xsl:template name="TATA_interrupt">
                Fonction TATA :
        <xsl:variable name="v1_name" select="'NaN'"/>
        <xsl:value-of select="$v1_name" />
    </xsl:template>
</xsl:stylesheet>

这是我的输入 XML:

<testspec>
   <command label="TOTO_short"/>
   <command label="TATA_interrupt"/>
   <command label="TOTO_short"/>
   <command label="TATA_interrupt"/>
</testspec>

我的问题如下:在模板 TOTO_short 和 TATA_short 中,我想定义 2 个变量并显示它们的值...

但它不起作用!

你能帮我理解它的来源吗?

提前非常感谢:)

阿尔诺

4

1 回答 1

2

问题在于这条看起来很疯狂的线

<xsl:apply-templates select="document($self)/xsl:stylesheet/xsl:template[@name = $current/@label]"/>

看起来您正在尝试使用与当前label属性相同的名称来调用命名模板。但是要调用命名模板,您必须使用xsl:call-template。为了让您当前的xsl:apply-templates找到任何东西,您需要一个像这样的模板:

<xsl:template match="xsl:template[@name='TOTO_short']">
    <xsl:call-template name="TOTO_short" />
</xsl:template>

这真的不是做事的方式!您的命名模板似乎工作的原因是这里使用了 XSLT 中的内置模板。当它找不到像上面这样的匹配模板时,它会简单地输出元素的文本。

我真的不认为需要这种复杂的方法。而不是您当前的xsl:for-each,您可以简单地执行以下操作:

<xsl:apply-templates select="$root//command" />

然后你有一个匹配的模板,像这样:

<xsl:template match="command[@label='TOTO_short']">
    <xsl:text>Fonction TOTO :</xsl:text>
    <xsl:variable name="dda" select="'b'"/>
    <xsl:copy-of select="$dda"/>
</xsl:template>

试试这个 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />
    <xsl:param name="basename"/>
    <xsl:param name="purpose"/>
    <xsl:param name="xml_input_path" select="'C:\Users\Tim Case\Documents\Test.xml'" />
    <xsl:param name="self"/>

    <xsl:template match="testspec">
        <xsl:call-template name="call_commands"/>
    </xsl:template>

    <xsl:template name="call_commands">
        <xsl:variable name="root" select="document($xml_input_path)/testspec"/>
        <xsl:apply-templates select="$root//command" />
    </xsl:template>

    <xsl:template match="command[@label='TOTO_short']">
        <xsl:text>Fonction TOTO :</xsl:text>
        <xsl:variable name="dda" select="'b'"/>
        <xsl:copy-of select="$dda"/>
    </xsl:template>

    <xsl:template match="command[@label='TATA_interrupt']">
                Fonction TATA :
        <xsl:variable name="v1_name" select="'NaN'"/>
        <xsl:value-of select="$v1_name" />
    </xsl:template>
</xsl:stylesheet>

实际上,我不确定您为什么在这里将 XML 的路径作为参数传递。您实际上可以将 XSLT 简化为:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />
    <xsl:param name="basename"/>
    <xsl:param name="purpose"/>

    <xsl:template match="testspec">
        <xsl:apply-templates select="command" />
    </xsl:template>

    <xsl:template match="command[@label='TOTO_short']">
        <xsl:text>Fonction TOTO :</xsl:text>
        <xsl:variable name="dda" select="'b'"/>
        <xsl:copy-of select="$dda"/>
    </xsl:template>

    <xsl:template match="command[@label='TATA_interrupt']">
                Fonction TATA :
        <xsl:variable name="v1_name" select="'NaN'"/>
        <xsl:value-of select="$v1_name" />
    </xsl:template>
</xsl:stylesheet>
于 2013-04-10T17:32:39.783 回答