Domdocument:为什么 XSLT 转换输出变成单行?
Domdocument: Why XSLT Transformation Output became single line?
您好,我想知道如何在使用 XSLT 时零售 xml 结构,
我在下面有这些代码,
/* XSLT File */
$xsl = new \DOMDocument;
$xsl->loadXML($xsltData[0]->xslt_template);
$xsl->preserveWhiteSpace = false;
$xsl->formatOutput = true;
/* Combine and Transform XML and XSLT */
$proc = new \XSLTProcessor;
$proc->importStyleSheet($xsl);
$proc->preserveWhiteSpace = false;
$proc->formatOutput = true;
$transformedOutPut = $proc->transformToXML($xml);
这是我的输入xml
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<titleA style="test" size="123">Kevin del </titleA>
<address>1119 Johnson Street, San Diego, California</address>
<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="titleA"/></titlenew>
<addressnew>
<street><xsl:value-of select="address/street"/></street>
<city><xsl:value-of select="address/city"/></city>
<state><xsl:value-of select="address/state"/></state>
</addressnew>
</cdnew>
</xsl:for-each>
</catalognew>
</xsl:template>
</xsl:stylesheet>
为什么它给我一个单行结果而不保留其原始结构,
<?xml version="1.0"?>
<catalognew><container-title>My Data</container-title><cdnew><titlenew>Kevin del </titlenew><addressnew><street>1119 JOHNSON STREET</street><city>SAN DIEGO</city><state>CALIFORNIA</state></addressnew></cdnew></catalognew>
希望有人能帮助我,
谢谢,
问题似乎是您需要确保最终文档对象具有格式化输出的设置。因此,与其使用 transformToXML($xml)
,这会创建一个新文档并确保该新文档在输出结果之前设置了格式选项...
$transformedOutPut = $proc->transformToDoc($xml);
$transformedOutPut->preserveWhiteSpace = true;
$transformedOutPut->formatOutput = true;
print_r($transformedOutPut->saveXML());
给...
<?xml version="1.0"?>
<catalognew>
<container-title>My Data</container-title>
<cdnew>
<titlenew>Kevin del </titlenew>
<addressnew>
<street/>
<city/>
<state/>
</addressnew>
</cdnew>
</catalognew>
您好,我想知道如何在使用 XSLT 时零售 xml 结构,
我在下面有这些代码,
/* XSLT File */
$xsl = new \DOMDocument;
$xsl->loadXML($xsltData[0]->xslt_template);
$xsl->preserveWhiteSpace = false;
$xsl->formatOutput = true;
/* Combine and Transform XML and XSLT */
$proc = new \XSLTProcessor;
$proc->importStyleSheet($xsl);
$proc->preserveWhiteSpace = false;
$proc->formatOutput = true;
$transformedOutPut = $proc->transformToXML($xml);
这是我的输入xml
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<titleA style="test" size="123">Kevin del </titleA>
<address>1119 Johnson Street, San Diego, California</address>
<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="titleA"/></titlenew>
<addressnew>
<street><xsl:value-of select="address/street"/></street>
<city><xsl:value-of select="address/city"/></city>
<state><xsl:value-of select="address/state"/></state>
</addressnew>
</cdnew>
</xsl:for-each>
</catalognew>
</xsl:template>
</xsl:stylesheet>
为什么它给我一个单行结果而不保留其原始结构,
<?xml version="1.0"?>
<catalognew><container-title>My Data</container-title><cdnew><titlenew>Kevin del </titlenew><addressnew><street>1119 JOHNSON STREET</street><city>SAN DIEGO</city><state>CALIFORNIA</state></addressnew></cdnew></catalognew>
希望有人能帮助我,
谢谢,
问题似乎是您需要确保最终文档对象具有格式化输出的设置。因此,与其使用 transformToXML($xml)
,这会创建一个新文档并确保该新文档在输出结果之前设置了格式选项...
$transformedOutPut = $proc->transformToDoc($xml);
$transformedOutPut->preserveWhiteSpace = true;
$transformedOutPut->formatOutput = true;
print_r($transformedOutPut->saveXML());
给...
<?xml version="1.0"?>
<catalognew>
<container-title>My Data</container-title>
<cdnew>
<titlenew>Kevin del </titlenew>
<addressnew>
<street/>
<city/>
<state/>
</addressnew>
</cdnew>
</catalognew>