1

转到此链接Convert XML file to attribute XML
and copy/past my question xml file (with name "xml_simple.xml")
and copy/past answer xslt then (with name "xslt4convert.xsl")
copy/past this php file
php code :-

<?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文件,如何删除它是在记事本 中显示 谢谢...
&#10;
&#10;

4

1 回答 1

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 &#10; entities with an empty
      string
      -->
      <xsl:value-of select="translate(., '&#10;', '')"/>
    </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>
于 2013-03-08T06:30:27.477 回答