转到此链接Convert XML file to attribute XML
并复制/过去我的问题xml文件(名称为“xml_simple.xml”)
然后复制/过去回答xslt(名称为“xslt4convert.xsl”)
复制/过去这个php文件
php代码: -
<?php
$sourcedoc = new DOMDocument();
$sourcedoc->load('xml_simple.xml');
$stylesheet = new DOMDocument();
$stylesheet->load('xslt4convert.xsl');
// create a new XSLT processor and load the stylesheet
$xsltprocessor = new XSLTProcessor();
$xsltprocessor->importStylesheet($stylesheet);
// save the new xml file
file_put_contents('xml_converted.xml', $xsltprocessor->transformToXML($sourcedoc));
echo ' xml convert';
?>
并运行此php文件,生成新的xml文件。将此文件打开到记事本
每个属性值都
它在浏览器中显示一个空格如何删除此
如果我将xml文件打开到记事本中,则显示在记事本中
感谢...
答案 0 :(得分:1)
这个样式表应该这样做:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<!--
Identity transform: copy all nodes that don't have overriding templates as is
-->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@value">
<xsl:attribute name="value">
<!--
Use the translate() function to replace all entities with an empty
string
-->
<xsl:value-of select="translate(., ' ', '')"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0"?>
<products>
<product_id value="1">
<tab_id value="351">
<tab_name value="test1"/>
<region_timezone value="1"/>
<dist_region value="5069,5069,5069"/>
<dist_value value="55,342,86"/>
<reg_str_dt value="2013-01-14 20:35:00"/>
</tab_id>
</product_id>
<product_id value="2">
<tab_id value="352">
<tab_name value="test2"/>
<region_timezone value="1"/>
<dist_region value="4457,7140,5069"/>
<dist_value value="55,213,86"/>
<reg_end_dt value="2013-02-14 20:39:00"/>
</tab_id>
</product_id>
</products>