如何从包含XML结构的变量中删除某些特定元素


PHP - How to delete from variable that contains XML structure some specific elements?

我有一个包含一些XML值的字段$xml。首先,我必须提到的是,元素不是在新行(行)中分开的,而是像没有新行的字符串一样结合在一起。

我将首先展示XML结构如何看起来容易"读"。

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv=" http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <p:queryBillingAccountResponse xmlns:p=" http://www.ibm.com">
            <ns0:customerAccount xmlns:ns0=" http://www.ibm.com/xmlns/">
                <AccountStatus>Paid</AccountStatus>
                <ComponentCustomerAccount>
                    <Name>ADSL 4</Name>
                    <CharacteristicValue>
                        <Characteristic>
                            <Name>Balance</Name>
                        </Characteristic>
                        <Value>0.0</Value>
                    </CharacteristicValue>
                    <AccountStatus>Paid</AccountStatus>
                </ComponentCustomerAccount>
            </ns0:customerAccount>
        </p:queryBillingAccountResponse>
    </soapenv:Body>
</soapenv:Envelope>
<AccountStatus>Paid</AccountStatus>
</ComponentCustomerAccount>
</ns0:customerAccount>
</p:queryBillingAccountResponse>
</soapenv:Body>
</soapenv:Envelope>

,但是我必须再次提到,$xml字段中的实际值不是那么容易读。例如,它看起来像这样

<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv=" http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><p:queryBillingAccountResponse xmlns:p=" http://www.ibm.com">.......

我想删除元素:?xmlversionsoapenv:Envelopesoapenv:Body及其属性。我想在xml值的开头和结尾删除它们。其他一切保持原样1如何实现这一点?所以我在php字段的新值应该从queryBillingAccountResponse元素开始。谢谢你

对于有效的XML,可以使用SimpeXMLDOMDocument来查询body元素的子节点。

$xml = '<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <p:queryBillingAccountResponse xmlns:p="http://www.ibm.com">
            <ns0:customerAccount xmlns:ns0="http://www.ibm.com/xmlns/">
                <AccountStatus>Paid</AccountStatus>
                <ComponentCustomerAccount>
                    <Name>ADSL 4</Name>
                    <CharacteristicValue>
                        <Characteristic>
                            <Name>Balance</Name>
                        </Characteristic>
                        <Value>0.0</Value>
                    </CharacteristicValue>
                    <AccountStatus>Paid</AccountStatus>
                </ComponentCustomerAccount>
            </ns0:customerAccount>
        </p:queryBillingAccountResponse>
    </soapenv:Body>
</soapenv:Envelope>';
$xml = simplexml_load_string($xml);
$xml = $xml->xpath('//soapenv:Body/child::*')[0];
echo $xml->asXML();

结果是:

<p:queryBillingAccountResponse xmlns:p="http://www.ibm.com">
    <ns0:customerAccount xmlns:ns0="http://www.ibm.com/xmlns/">
        <AccountStatus>Paid</AccountStatus>
        <ComponentCustomerAccount>
            <Name>ADSL 4</Name>
            <CharacteristicValue>
                <Characteristic>
                    <Name>Balance</Name>
                </Characteristic>
                <Value>0.0</Value>
            </CharacteristicValue>
            <AccountStatus>Paid</AccountStatus>
        </ComponentCustomerAccount>
    </ns0:customerAccount>
</p:queryBillingAccountResponse>

但问题是你的XML是无效的,我不知道它是否是一个复制粘贴错误

这是使用XSLT最简单、最万无一失的方法:

function extract_body_stripns($xmlstring) {
    static $xsl = NULL;
    if ($xsl === NULL) {
        $xsl_soap_body_nons = <<<'EOT'
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output encoding="UTF-8" method="xml" />
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="*[namespace-uri()]" priority="1">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="@*[namespace-uri()]" priority="1">
    <xsl:attribute name="{local-name()}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
  <xsl:template match="/">
    <xsl:apply-templates select="/soapenv:Envelope/soapenv:Body/*"/>
  </xsl:template>
</xsl:stylesheet>
EOT;
        $style = new DOMDocument();
        $style->loadXML($xsl_soap_body_nons, LIBXML_COMPACT | LIBXML_NOBLANKS | LIBXML_NONET);
        $xsl = new XSLTProcessor();
        $xsl->importStylesheet($style);
        unset($style);
    }
    $d = new DOMDocument();
    $d->loadXML($xmlstring, LIBXML_COMPACT | LIBXML_NONET);
    $newd = $xsl->transformToDoc($d);
    unset($d);
    return $newd->saveXML($newd->documentElement);
}

使用这个函数:

echo extract_body_stripns($xmlString);

结果是:

<queryBillingAccountResponse>
        <customerAccount>
            <ComponentCustomerAccount>
                <Name>ADSL 4</Name>
                <CharacteristicValue>
                    <Characteristic>
                        <Name>Balance</Name>
                    </Characteristic>
                    <Value>0.0</Value>
                </CharacteristicValue>
                <AccountStatus>Paid</AccountStatus>
            </ComponentCustomerAccount>
        </customerAccount>
    </queryBillingAccountResponse>

注意,如果在源文档中有命名空间属性,剥离命名空间的过程可能会导致丢失其中一些属性。例如,对于元素<myelement ns:myattrib="a" myattrib="b"/>,您的一个属性将丢失,而您将丢失的属性是不一致的!

您可以使用ereg_replacehttp://php.net/manual/en/function.ereg-replace.php

使用regex标识要删除的元素。