0

xpath を使用して xml を別の xml 形式に変換するにはどうすればよいですか? xml ファイルは動的で、任意のレベル、異なるタグである可能性があります。次の xml はサンプルです。

<?xml version="1.0"?> 
<student_info>     
<total_stud>500</total_stud>     
  <student>         
    <id>1</id>         
    <name>abc</name>         
    <address>             
       <city>Pune</city>             
       <zip>411006</zip>         
    </address>     
  </student>     
  <student>         
    <id>1</id>         
    <name>bbc</name>         
    <address>             
      <city>Toronto</city>             
      <zip>82233</zip>         
    </address>     
  </student> 
  <student>         
    <id>2</id>         
    <name>wec</name>         
    <address>             
      <city>North York</city>             
      <zip>59522</zip>         
    </address>     
  </student> 
</student_info> 

To: 全学生 "id=1"

<?xml version="1.0"?>
<student_info>
  <student>
    <name>abc</name>
    <city>Pune</city>             
    <zip>411006</zip>      
  </student> 
  <student>
    <name>bbc</name>
    <city>Toronto</city>             
    <zip>82233</zip>      
  </student>
</student_info>
4

2 に答える 2

2

おそらく、XPath を容易にする XSL 変換を使用したいと思うでしょう。

次のように PHP から使用できます: http://www.php.net/manual/en/book.xsl.php

この例の XSL は次のようになります。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>

  <xsl:param name="id" />

  <xsl:template match="/student_info">
    <xsl:copy>
      <xsl:apply-templates select=".//student[id=$id]"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="student">
    <xsl:copy>
      <xsl:copy-of select="name" />
      <xsl:copy-of select="address/city" />
      <xsl:copy-of select="address/zip" />
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

idパラメーター (および を使用してスタイルシートで使用<xsl:param>)$idを を使用してトランスフォーマーに渡すことができますsetParameter

編集: これは xsltCake のスライスです: http://www.xsltcake.com/slices/dnuFXh 残念ながら、パラメーターをサポートしていません。

于 2012-07-07T23:17:01.350 に答える
1

これを試して:

// ファイルに保存: ts1.xml

<?xml version="1.0" encoding="utf-8"?>
<student_info>     
    <total_stud>500</total_stud>     
  <student>         
    <id>1</id>         
    <name>abc</name>         
    <address>             
       <city>Pune</city>             
       <zip>411006</zip>         
    </address>     
  </student>     
  <student>         
    <id>1</id>         
    <name>bbc</name>         
    <address>             
      <city>Toronto</city>             
      <zip>82233</zip>         
    </address>     
  </student> 
  <student>         
    <id>2</id>         
    <name>wec</name>         
    <address>             
      <city>North York</city>             
      <zip>59522</zip>         
    </address>     
  </student> 
</student_info>

ここにPHPコード:

<?php
$xml = simplexml_load_file( 'ts1.xml' );

// search all nodes with id=1
$xpath = ( $xml->xpath( '//student/id[.=1]/..' ) );

// create a new XML
$newXML = simplexml_load_string( "<?xml version=\"1.0\" encoding=\"utf-8\"?><student_info></student_info>" );

// add matching xml nodes to it.
foreach( $xpath as $st ) {
    $student = $newXML->addChild( 'student' );
    $student->addChild( 'name', (string)$st->name );
    $student->addChild( 'city', (string)$st->address->city );
    $student->addChild( 'zip', (string)$st->address->zip );
}

$xmldoc = new DomDocument( '1.0' );
$xmldoc->preserveWhiteSpace = false;
$xmldoc->formatOutput = true;

$xmldoc->loadXML( $newXML->asXML(), LIBXML_NOBLANKS );
//save as 1.xml
$xmldoc->save( '1.xml' );
?>

1.xml に保存された結果

<?xml version="1.0" encoding="utf-8"?>
<student_info>
  <student>
    <name>abc</name>
    <city>Pune</city>
    <zip>411006</zip>
  </student>
  <student>
    <name>bbc</name>
    <city>Toronto</city>
    <zip>82233</zip>
  </student>
</student_info>

これにより、id=1 のすべてのノードに対して新しい xml が作成されます。必要に応じて変更できます。お役に立てれば。

于 2012-07-04T07:08:06.973 に答える