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
XSLT:当只有外部XSL标记时,为什么XSLT不生成输出_Xslt - Fatal编程技术网

XSLT:当只有外部XSL标记时,为什么XSLT不生成输出

XSLT:当只有外部XSL标记时,为什么XSLT不生成输出,xslt,Xslt,我真正的代码不是这个,但我在这里陈述的问题适用于我真正的代码 XML: <?xml version="1.0" encoding="UTF-8"?> <books> <book.child.1> <title>charithram</title> <author>sarika</author> </book.child.1> <book.c

我真正的代码不是这个,但我在这里陈述的问题适用于我真正的代码

XML:

<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book.child.1>
        <title>charithram</title>
        <author>sarika</author>
    </book.child.1>
    <book.child.2>
        <title>doublebell</title>
        <author>psudarsanan</author>
    </book.child.2>
</books>
 <?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8"
        indent="yes" />
    <xsl:template match="/">
        <mytag>
            <xsl:for-each select="books/*">
                <newbook>
                    <title>
                        <xsl:value-of select="title" />
                    </title>
                </newbook>
            </xsl:for-each>
        </mytag>
    </xsl:template>
</xsl:stylesheet>

查里瑟姆

我不明白出了什么问题,最后当我修改XSLT时,如下面所述

XSLT2:

<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book.child.1>
        <title>charithram</title>
        <author>sarika</author>
    </book.child.1>
    <book.child.2>
        <title>doublebell</title>
        <author>psudarsanan</author>
    </book.child.2>
</books>
 <?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8"
        indent="yes" />
    <xsl:template match="/">
        <mytag>
            <xsl:for-each select="books/*">
                <newbook>
                    <title>
                        <xsl:value-of select="title" />
                    </title>
                </newbook>
            </xsl:for-each>
        </mytag>
    </xsl:template>
</xsl:stylesheet>

在这种情况下生成输出

outputXML:

<?xml version="1.0" encoding="UTF-8"?>
<mytag>
    <newbook>
        <title>charithram</title>
    </newbook>
    <newbook>
        <title>doublebell</title>
    </newbook>
</mytag>

查里瑟姆
双铃
你能解释一下为什么会有这种行为吗


另外,我不知道如何确切地问这个问题,所以如果我需要更改问题标题,请编辑或让我知道。

您的第一个XSLT理论上将生成输出

<?xml version="1.0" encoding="UTF-8"?>
<newbook>
    <title>charithram</title>
</newbook>
<newbook>
    <title>doublebell</title>
</newbook>

查里瑟姆
双铃
但该输出不是有效的XML,因为它有两个根标记,这不是格式良好的XML

在这种情况下,您可能有以下选择

  • 像在XSLT2中那样指定根元素

  • 将输出从XML更改为文本,但请注意,任何XML程序都无法读取输出

  • <?xml version="1.0" encoding="UTF-8"?>
    <newbook>
        <title>charithram</title>
    </newbook>
    <newbook>
        <title>doublebell</title>
    </newbook>
    

明白了,这不是XSLT的语法问题!!。谢谢Thomas,我没有想到XML的基本概念“XML文档必须包含一个元素,它是所有其他元素的父元素。这个元素称为根元素”。谢谢