Sri P Sri P - 1 month ago 15
PHP Question

remove empty xmlns="" attribute using php

I have tried all the possible ways to remove the empty xmlns from tags, Please help to solve this issue.

$XMLDoc = new DOMDocument('1.0', 'UTF-8');
$XMLDoc->preserveWhiteSpace = false;
$XMLDoc->formatOutput = true;
$soap = $XMLDoc->createElementNS('http://schemas.xmlsoap.org/soap/envelope/', 'soap:Envelope');
$XMLDoc->appendChild($soap);
$soap->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$soap->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:xsd', 'http://www.w3.org/2001/XMLSchema');
$soap->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$body = $XMLDoc->createElementNS('http://schemas.xmlsoap.org/soap/envelope/', 'soap:Body');
$XMLDoc->appendChild($body);
include 'DBDetails.php';
$XMlQuery="SELECT `First_Name`, `Last_Name`, `Email`, `Pan_No`, `Res_address`, `Res_address2`, `Res_address3`, `Resi_type`, `Mobile`, `Res_City`, `Resi_City_other`, `Resi_City_other1`, `res_pin`, `Company_name`, `DateOfBirth`, `Designation`, `Emp_type`, `Monthly_income`, `card_held`, `Source_code`, `Promo_code`, `LEAD_DATE_TIME`, `PRODUCT_APPLIED_FOR`, `existingcust`, `LoanAmt`, `YrsinEmp`, `emi_paid`, `car_make`, `car_model`, `TypeOfLoan`, `IP_Address`, `Indigo_UniqueKey`, `Indigo_RequestFromYesNo` FROM `webservice` WHERE `Source_code` LIKE '$Sourcecode'";
$rowcount=-1;
if($result=mysqli_query($conn,$XMlQuery)) {
$rowcount=mysqli_num_rows($result);
}
if($rowcount>0){
$StockCount=-1;
$rootElement = $XMLDoc->createElement('AddDetails');
$rootNode=$body->appendChild($rootElement);
while($result_array = $result->fetch_assoc()) {
$StockCount++;
foreach($result_array as $key => $value) {
$value=trim($value);
if($value=="NULL" || $value=="" ||$value==-1){
$value="";
}
$rootNode->appendChild($XMLDoc->createElement($key,$value));
}
}
mysqli_close($conn);
}
$XSLDoc = new DOMDocument('1.0', 'UTF-8');
$xslstr = '<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<xsl:apply-templates select="@*|node()" />
</xsl:template>
<xsl:template match="AddDetails">
<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>
<AddDetails xmlns="http://tempuri.org/">
<xsl:copy-of select="*" />
</AddDetails>
</soap:Body>
</soap:Envelope>
</xsl:template>
</xsl:transform>';
$XSLDoc->loadXML($xslstr);
$proc = new XSLTProcessor;
$proc->importStyleSheet($XSLDoc);
$newXML = $proc->transformToXML($XMLDoc);
echo $newXML;


It's resulting out as

<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>
<AddDetails xmlns="http://tempuri.org/">
<First_Name xmlns="">TestFName</First_Name>
<Last_Name xmlns="">TestLName</Last_Name>
<Email xmlns="">test@gfdg.co.in</Email>
</AddDetails>
</soap:Body>
</soap:Envelope>


I would like to generate the XML like this

<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>
<AddDetails xmlns="http://tempuri.org/">
<First_Name>TestFName</First_Name>
<Last_Name>TestLName</Last_Name>
<Email>test@gfdg.co.in</Email>
</AddDetails>
</soap:Body>
</soap:Envelope>


Please help me. i want to generate the code without empty xmlns.

Answer

Quick and dirty patch.

Just before the last line:

echo $newXML;

Add this:

$newXML = str_replace('xmlns=""', '', $newXML);
Comments