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
Templates 有条件地选择不工作的节点_Templates_Xslt - Fatal编程技术网

Templates 有条件地选择不工作的节点

Templates 有条件地选择不工作的节点,templates,xslt,Templates,Xslt,我只是尝试选择属性值为“cd”的节点“productgroep”。这不起作用,我真的不明白为什么,我搜索了答案,但没有找到任何答案 <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="Oefening_8.xsl"?> <catalogus> <!-- cd catalogus --> <productgroep type="cd">

我只是尝试选择属性值为“cd”的节点“productgroep”。这不起作用,我真的不明白为什么,我搜索了答案,但没有找到任何答案

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Oefening_8.xsl"?>
<catalogus>
<!-- cd catalogus -->
<productgroep type="cd">
    <item referentienummer="7051444" catalogusnummer="1800022" EAN="0025218000222">
     ...
</productgroep>
<productgroep type="film">
    <item referentienummer="8051445" catalogusnummer="2800023" EAN="5051888073650">
     ....
</productgroep>
</catalogus

...
....
不能是
的子项,因此您的样式表当前无效,可能在某个地方出错,具体取决于您使用XML和XSL的方式

一种解决方案是创建单独的
并使用
处理源元素的子元素

<xsl:template match="/">
    <html>
        <head>
            <title>Oefening_8.xsl</title>
            <meta charset="utf-8"/>
            <link href="Oefening_8.css" type="text/css" rel="stylesheet"/>
        </head>
        <body>
            <h1></h1>
            <xsl:apply-templates />
        </body>
    </html>
</xsl:template>

<xsl:template match="productgroep[@type='cd']">
    <xsl:value-of select="item/@catalogusnummer"/> <!-- print @catalogusnummer for example -->
</xsl:template>

Oefening_8.xsl

正如@andyb所指出的,模板中不能有模板。可能您打算在有
xsl:template
的地方使用
xsl:apply templates
,但这对您使用的路径也不起作用,因为当前上下文是目录上方的节点。您可以选择更改初始的
xsl:template
,以使用以下任一选项选择根元素:

或者使用
xsl:apply templates
中的完整路径:

我更喜欢第一种选择:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/*">   
    <html>
        <head>
            <title>Oefening_8.xsl</title>
            <meta charset="utf-8"/>
            <link href="Oefening_8.css" type="text/css" rel="stylesheet"/>
        </head>
        <body>
            <h1></h1>
            <xsl:apply-templates select="productgroep[@type='cd']" />
        </body>
    </html>
</xsl:template>
</xsl:stylesheet>

Oefening_8.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/*">   
    <html>
        <head>
            <title>Oefening_8.xsl</title>
            <meta charset="utf-8"/>
            <link href="Oefening_8.css" type="text/css" rel="stylesheet"/>
        </head>
        <body>
            <h1></h1>
            <xsl:apply-templates select="productgroep[@type='cd']" />
        </body>
    </html>
</xsl:template>
</xsl:stylesheet>