简体   繁体   English

PHP更改XML节点值

[英]PHP Change XML node values

I'm having some difficulties in changing XML Node values with PHP. 我在使用PHP更改XML节点值时遇到一些困难。

My XML is the following 我的XML如下

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
            <ProcessTransaction
                  xmlns="http://example.com">
                  <TransactionRequest
                        xmlns="http://example.com">
                        <Header>
                              <RequestType>SALE</RequestType>
                              <RequestMethod>SYNCHRONOUS</RequestMethod>
                              <MerchantInfo>
                                    <PosName>kwstasna</PosName>
                                    <PosID>1234</PosID>
                              </MerchantInfo>
                        </Header>
                  </TransactionRequest>
            </ProcessTransaction>
      </soap:Body>
</soap:Envelope> 

And i want to change PosName and PosID . 而且我想更改PosNamePosID The XML is received from a POST Request. 从POST请求接收XML。 If i print_r($REQUEST['xml'] I get the values in text. 如果我print_r($REQUEST['xml']我得到文本中的值。

And what i've tried is the following 我尝试过的是以下

$posid = '321';
$posname = 'nakwsta';

$result = $xml->xpath("/soap:Envelope/soap:Body/ProcessTransaction/TransactionRequest/Header/MerchantInfo");

$result[0]->PosID = $posid;
$result[0]->PosName = $posname;

echo $result;

But i get an empty array Array[] 但是我得到一个空数组Array[]

I think my mistake is in the values of <soap:Envelope for example. 我认为我的错误在于例如<soap:Envelope的值。 Anyone that had the same issue and find out the way to solve it? 是否有人遇到相同的问题并找到解决方法?

Thanks a lot for your time. 非常感谢您的宝贵时间。

The ProcessTransaction element (and all of its child nodes) are in the " http://example.com " namespace. ProcessTransaction元素(及其所有子节点)位于http://example.com命名空间中。 If you want to access them using xpath() , you'll need to register a namespace prefix: 如果要使用xpath()访问它们,则需要注册一个名称空间前缀:

$xml->registerXPathNamespace('ex', 'http://example.com');

You can then use the ex prefix on all relevant parts of your query 然后,您可以在查询的所有相关部分使用ex前缀

$result = $xml->xpath("/soap:Envelope/soap:Body/ex:ProcessTransaction/ex:TransactionRequest/ex:Header/ex:MerchantInfo");

The rest of your code should function correctly, see https://eval.in/916856 您的其余代码应正常运行,请参见https://eval.in/916856

Consider a parameterized XSLT (not unlike parameterized SQL) where PHP passes value to the underlying script with setParameter() . 考虑一个参数化的XSLT(与参数化SQL相同),其中PHP使用setParameter()将值传递给底层脚本。

As information, XSLT (sibling to XPath) is a special-purpose language designed to transform XML files. 作为信息, XSLT (与XPath相同)是一种专用语言,旨在转换XML文件。 PHP can run XSLT 1.0 scripts with the XSL class. PHP可以使用XSL类运行XSLT 1.0脚本。 Specifically, below runs the Identity Transform to copy XML as is and then rewrites the PosName and PosID nodes. 具体来说,下面运行Identity Transform 原样复制XML,然后重写PosNamePosID节点。 Default namespace is handled accordingly in top root tag aligned to doc prefix. 默认名称空间在与doc前缀对齐的顶部根标记中进行相应处理。

XSLT (save as .xsl file, a special well-formed .xml file) XSLT (另存为.xsl文件,格式特殊的.xml文件)

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

  <xsl:param name="PosNameParam"/>
  <xsl:param name="PosIDParam"/>

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

  <!-- RE-WRITE PosName NODE -->
  <xsl:template match="doc:PosName">    
    <xsl:copy>
       <xsl:value-of select="$PosNameParam"/>
    </xsl:copy>
  </xsl:template>

  <!-- RE-WRITE PosID NODE -->
  <xsl:template match="doc:PosID">
    <xsl:copy>
        <xsl:value-of select="$PosIDParam"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

PHP 的PHP

$posid = '321';
$posname = 'nakwsta';

// Load XML and XSL
$xml = new DOMDocument;
$xml->load('Input.xml');

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

// Configure transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); 

// Assign values to XSLT parameters
$proc->setParameter('', 'PosNameParam', $posid);
$proc->setParameter('', 'PosIDParam', $posname);

// Transform XML source
$newXML = new DOMDocument;
$newXML = $proc->transformToXML($xml);

// Output to console
echo $newXML;

// Output to file
file_put_contents('Output.xml', $newXML);

Output 输出量

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
            <ProcessTransaction xmlns="http://example.com">
                  <TransactionRequest>
                        <Header>
                              <RequestType>SALE</RequestType>
                              <RequestMethod>SYNCHRONOUS</RequestMethod>
                              <MerchantInfo>
                                    <PosName>nakwsta</PosName>
                                    <PosID>321</PosID>
                              </MerchantInfo>
                        </Header>
                  </TransactionRequest>
            </ProcessTransaction>
      </soap:Body>
</soap:Envelope>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM