如何在 PHP 中通过 xpath 根据标签值将一个 xml 转换为另一个 xml


How can convert one xml to another xml based on the tag value by xpath in PHP?

如何使用 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> 

收件人:所有学生"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>
您可能

希望使用 XSL 转换,它有助于 XPath。

你可以像这样从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>

您可以使用 setParameterid 参数(使用 <xsl:param>$id 与样式表一起使用)传递给转换器。

编辑:这是一片xsltCake可以玩: http://www.xsltcake.com/slices/dnuFXh 不幸的是,它不支持参数。

试试这个:

将其保存在一个文件中: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。您可以对其进行修改以满足您的需求。希望这有帮助。