2

私はこのタイプのxmlファイルを持っています:-

<product>
   <node>
       <region_id>
                <node>1</node>
       </region_id>
       <region_time>
                <node>27</node>
                <node>02</node>
                <node>2013</node>
       </region_time>
   </node>
</ptroduct>

私はこのタイプのようにそれらを変更したい:-

<product>
      <region_id>1</region_id>
      <region_time>27,02,2013</region_time>
</product>

<Node>上記のように値が欲しいだけで削除したい

4

3 に答える 3

4

この変換:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="node">
  <xsl:apply-templates/>
 </xsl:template>

 <xsl:template match="node[position()>1]/text()">
   <xsl:text>,</xsl:text>
   <xsl:value-of select="."/>
 </xsl:template>
</xsl:stylesheet>

提供された XML ドキュメントに適用した場合:

<product>
    <node>
        <region_id>
            <node>1</node>
        </region_id>
        <region_time>
            <node>27</node>
            <node>02</node>
            <node>2013</node>
        </region_time></node>
</product>

必要な正しい結果が生成されます。

<product>
   <region_id>1</region_id>
   <region_time>27,02,2013</region_time>
</product>
于 2013-02-27T06:00:12.713 に答える
2

次の例は、その仕事をします。

次のファイルがあるとします。

test.xml

<?xml version="1.0"?>
<product>
   <node>
       <region_id>
                <node>1</node>
       </region_id>
       <region_time>
                <node>27</node>
                <node>02</node>
                <node>2013</node>
       </region_time>
   </node>
</product>

test.xsl 更新

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:strip-space elements="*"/>

 <xsl:template match="*">
  <xsl:copy>
   <xsl:apply-templates select="*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="node">
  <xsl:apply-templates/>
 </xsl:template>

 <xsl:template match="/">
    <xsl:apply-templates />
 </xsl:template>

 <!-- from dimitre\'s xsl.thanks -->
 <xsl:template match="node[position()>1]/text()">
   <xsl:text>,</xsl:text>
   <xsl:value-of select="."/>
 </xsl:template>
</xsl:stylesheet>

xslt.php

$sourcedoc = new DOMDocument();
$sourcedoc->load('test.xml');

$stylesheet = new DOMDocument();
$stylesheet->load('test.xsl');

// create a new XSLT processor and load the stylesheet
$xsltprocessor = new XSLTProcessor();
$xsltprocessor->importStylesheet($stylesheet);

// save the new xml file
file_put_contents('test-translated.xml', $xsltprocessor->transformToXML($sourcedoc));
于 2013-02-27T05:59:53.193 に答える
0

次のことを行うスクリプトを書くことができます

  1. すべてのスペースと改行文字を削除します (SO、すべての内容が 1 行にあるように)。
  2. 文字列置換を使用して'</node><node>'、「,」に置き換えます
  3. 文字列置換を使用して'</node>'、'' および '''<node>'に置き換えます

終わり!!

これは、適切な構文を使用して、PHP または Javascript で使用できます。

于 2013-02-27T05:51:43.363 に答える