使用php删除空的xmlns =“”属性

时间:2016-07-20 11:37:18

标签: php xml

我已经尝试了从标签中删除空xml的所有可能方法,请帮助解决此问题。

$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;

结果是

<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>

我想像这样生成XML

<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>

请帮帮我。我想生成没有空xmlns的代码。

2 个答案:

答案 0 :(得分:0)

快速而肮脏的补丁。

就在最后一行之前:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not (Intersect(Target, Target.Worksheet.Range("H11")) Is Nothing) Then
        'do you coding here
    End If
End Sub

添加:

echo $newXML;

答案 1 :(得分:0)

这是由XSLT字符串中缺少的名称空间声明引起的。

如果XML文档中的命名空间未添加到XSLT文档中,则处理器会添加空白的默认xmlns属性。

尝试将XML文件中的所有命名空间添加到XSLT字符串中,这应解决空白的xmlns属性问题。