Reputation: 3
I'm having difficulty in removing whitespaces between XML opening and closing tags and well as between tags. As you can see from the XML code provided that there is whitespace inside the < priip> tag and several others while the closing tag for also has whitespace < /properties>
I have tried several modifications of preg_replace(), trim () but can't seem to get this working appropriately. I understand that usually, one should reject invalid XML rather than try to fix it but I have to fix this.
XML
< priip>
< data>
< product>
< priipCloudProductTemplate>otc</priipCloudProductTemplate>
< priipCloudProductType>fxSwap</priipCloudProductType>
< productIdentifier>RBI_fxSwap_EURUSD_long_1Y2D_EUR</productIdentifier> < /product>
< document>
< type>final</type>
< /document>
< properties>
< includeEarlyRedemptionInExtraordinaryEventsAlert>true</includeEarlyRedemptionInExtraordinaryEventsAlert>
< /properties>
< tradeDate>2018-01-18</tradeDate>
< effectiveDate>2018-01-20</effectiveDate>
< fxSwap>
<holder>client</holder>
<currencyPair>EURUSD</currencyPair>
<notionalAmount>1000000</notionalAmount>
<notionalAmountCurrency>EUR</notionalAmountCurrency>
<terminationDate>2019-01-20</terminationDate>
<forwardRate>
<value>1.25620</value>
</forwardRate> .......
I expect the output to be without unnecessary whitespaces
Output XML
<priip>
<data>
<product>
<priipCloudProductTemplate>otc</priipCloudProductTemplate>
<priipCloudProductType>fxSwap</priipCloudProductType>
<productIdentifier>RBI_fxSwap_EURUSD_long_1Y2D_EUR</productIdentifier></product>
<document>
<type>final</type>
</document>
<properties> <includeEarlyRedemptionInExtraordinaryEventsAlert>true</includeEarlyRedemptionInExtraordinaryEventsAlert>
</properties>
<tradeDate>2018-01-18</tradeDate>
<effectiveDate>2018-01-20</effectiveDate>
<fxSwap>
<holder>client</holder>
<currencyPair>EURUSD</currencyPair>
<notionalAmount>1000000</notionalAmount>
<notionalAmountCurrency>EUR</notionalAmountCurrency>
<terminationDate>2019-01-20</terminationDate>
<forwardRate>
<value>1.25620</value>
</forwardRate> ......
Upvotes: 0
Views: 380
Reputation: 38502
You can do it with simple php str_replace()
with an array mapping to remove the >space
with >
and <space
with <
$expected_xml = str_replace(['< ','> '],['<','>'],$xml);
echo $expected_xml;
DEMO: https://3v4l.org/Jq5Mi
Upvotes: 0
Reputation: 430
Maybe something like this...
$xml = '< priip>
< data>
< product>
< priipCloudProductTemplate>otc</priipCloudProductTemplate>
< priipCloudProductType>fxSwap</priipCloudProductType>
< productIdentifier>RBI_fxSwap_EURUSD_long_1Y2D_EUR</productIdentifier> < /product>
< document>
< type>final</type>
< /document>
< properties>
< includeEarlyRedemptionInExtraordinaryEventsAlert>true</includeEarlyRedemptionInExtraordinaryEventsAlert>
< /properties>
< tradeDate>2018-01-18</tradeDate>
< effectiveDate>2018-01-20</effectiveDate>
< fxSwap>
<holder>client</holder>
<currencyPair>EURUSD</currencyPair>
<notionalAmount>1000000</notionalAmount>
<notionalAmountCurrency>EUR</notionalAmountCurrency>
<terminationDate>2019-01-20</terminationDate>
<forwardRate>
<value>1.25620</value>
</forwardRate>';
echo preg_replace('/(<)\s(\/?(?:[A-Z][A-Z\d]*)\b[^>]*>)/i', '$1$2', $xml);
/*
* Will produce...
<priip>
<data>
<product>
<priipCloudProductTemplate>otc</priipCloudProductTemplate>
<priipCloudProductType>fxSwap</priipCloudProductType>
<productIdentifier>RBI_fxSwap_EURUSD_long_1Y2D_EUR</productIdentifier> </product>
<document>
<type>final</type>
</document>
<properties>
<includeEarlyRedemptionInExtraordinaryEventsAlert>true</includeEarlyRedemptionInExtraordinaryEventsAlert>
</properties>
<tradeDate>2018-01-18</tradeDate>
<effectiveDate>2018-01-20</effectiveDate>
<fxSwap>
<holder>client</holder>
<currencyPair>EURUSD</currencyPair>
<notionalAmount>1000000</notionalAmount>
<notionalAmountCurrency>EUR</notionalAmountCurrency>
<terminationDate>2019-01-20</terminationDate>
<forwardRate>
<value>1.25620</value>
</forwardRate>
*/
Will work even whit tags who cotains attributes like < tag arg="val">
, and the other solution will not, unfortunately i can't comment his answer bacause of my low reputation...
Upvotes: 1
Reputation: 758
I think its can help you.
<?php
$xml = <<<XML
< priip>
< data>
< product>
< priipCloudProductTemplate>otc</priipCloudProductTemplate>
< priipCloudProductType>fxSwap</priipCloudProductType>
< productIdentifier>RBI_fxSwap_EURUSD_long_1Y2D_EUR</productIdentifier> < /product>
< document>
< type>final</type>
< /document>
< properties>
< includeEarlyRedemptionInExtraordinaryEventsAlert>true</includeEarlyRedemptionInExtraordinaryEventsAlert>
< /properties>
< tradeDate>2018-01-18</tradeDate>
< effectiveDate>2018-01-20</effectiveDate>
< fxSwap>
<holder>client</holder>
<currencyPair>EURUSD</currencyPair>
<notionalAmount>1000000</notionalAmount>
<notionalAmountCurrency>EUR</notionalAmountCurrency>
<terminationDate>2019-01-20</terminationDate>
<forwardRate>
<value>1.25620</value>
</forwardRate> .......
XML;
$x = preg_replace('/(<)(\s+)\/?(\w+\>)/m', "$1$3", $xml);
echo $x;
?>
Return XML
<priip>
<data>
<product>
<priipCloudProductTemplate>otc</priipCloudProductTemplate>
<priipCloudProductType>fxSwap</priipCloudProductType>
<productIdentifier>RBI_fxSwap_EURUSD_long_1Y2D_EUR</productIdentifier> <product>
<document>
<type>final</type>
<document>
<properties>
<includeEarlyRedemptionInExtraordinaryEventsAlert>true</includeEarlyRedemptionInExtraordinaryEventsAlert>
<properties>
<tradeDate>2018-01-18</tradeDate>
<effectiveDate>2018-01-20</effectiveDate>
<fxSwap>
<holder>client</holder>
<currencyPair>EURUSD</currencyPair>
<notionalAmount>1000000</notionalAmount>
<notionalAmountCurrency>EUR</notionalAmountCurrency>
<terminationDate>2019-01-20</terminationDate>
<forwardRate>
<value>1.25620</value>
</forwardRate> .......
Upvotes: 1