首页 > 解决方案 > 为什么 XSLT 转换输出有尾随数字?

问题描述

我正在将我的 XML 转换为另一个 XML 文件并应用 XSLT 转换,但是转换的输出始终具有尾随数字 1。

这是我的代码

        /**
        ** Input XML 
        */
        $xml = new \DOMDocument;
        $xml->load($file1);
        /**
        ** XSLT File 
        **/
        $xsl = new \DOMDocument;
        $xsl->loadXML($file2);
        /**
        ** Combine and Transform XML and XSLT 
        **/
        $proc = new \XSLTProcessor;
        $proc->importStyleSheet($xsl); 
        $transformedOutPut = $proc->transformToXML($xml);

        return print_r($transformedOutPut); 

这是我的 XML 输入,

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
</catalog>

我有一个这样的 XSLT 文件,

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <catalognew>
    <container-title>My Data</container-title>
    <xsl:for-each select="catalog/cd">
    <cdnew>
      <titlenew><xsl:value-of select="title"/></titlenew>
      <artistnew><xsl:value-of select="artist"/></artistnew>
    </cdnew>
    </xsl:for-each>
  </catalognew>
</xsl:template>
</xsl:stylesheet>

这是输出结果

<?xml version="1.0"?>
<catalognew>
    <container-title>My Data</container-title>
    <cdnew>
        <titlenew>Empire Burlesque</titlenew>
        <artistnew>Bob Dylan</artistnew>
    </cdnew>
</catalognew>
1

我想知道为什么输出上方有一个额外的换行符,文件末尾有一个尾随数字 1,

我希望有人可以帮助我这是我第一次使用此功能。

标签: phpxmlxslt

解决方案


推荐阅读