Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从xml中删除命名空间别名_Xml - Fatal编程技术网

从xml中删除命名空间别名

从xml中删除命名空间别名,xml,Xml,我需要从xml中删除名称空间别名。此xml是从后端服务接收的(不同的响应可能具有不同的结构),最终需要转换为Json格式。 因此,在将xml转换为Jsonx和Json之前,我正在寻找一种通用XSLT来删除xml中的名称空间别名 XML我目前拥有: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <ns4:polic

我需要从xml中删除名称空间别名。此xml是从后端服务接收的(不同的响应可能具有不同的结构),最终需要转换为Json格式。 因此,在将xml转换为Jsonx和Json之前,我正在寻找一种通用XSLT来删除xml中的名称空间别名

XML我目前拥有:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns4:policySnapshotResponse xmlns="http://www.aig.com/ACORD1/xml/" xmlns:ns4="http://www.aig.com/gct/services/PolicyInquiryServiceV1.0" xmlns:ns2="http://www.ACORD.org/standards/PC_Surety/ACORD1/xml/" xmlns:ns3="http://www.aig.com/gct/services/commonHeaderV1.0">
         <ns4:requestHeader>
            <ns3:id>Spoofy</ns3:id>
            <ns3:requestApplicationID/>
            <ns3:requestMessageID/>
            <ns3:echoBack>false</ns3:echoBack>
         </ns4:requestHeader>
         <ns4:applicationContext>
            <ns3:businessSegment>CL</ns3:businessSegment>
            <ns3:region>ALL</ns3:region>
            <ns3:knowledgeDate>2015-05-24</ns3:knowledgeDate>
            <ns3:country>IE</ns3:country>
            <ns3:language>en</ns3:language>
            <ns3:lineOfBusiness>aig:CAUSC</ns3:lineOfBusiness>
            <ns3:subLineOfBusiness>AUTOP</ns3:subLineOfBusiness>
            <ns3:systemDate>2015-06-03</ns3:systemDate>
            <ns3:targetSystemName>GOALD</ns3:targetSystemName>
         </ns4:applicationContext>
         <ns4:PolicyInqRs>
            <ns2:PolInfo>
               <ns2:CommlPropertyPolicy>
                  <ns2:CommlPolicy/>
               </ns2:CommlPropertyPolicy>
            </ns2:PolInfo>
         </ns4:PolicyInqRs>
      </ns4:policySnapshotResponse>
   </soapenv:Body>
</soapenv:Envelope>

惊吓
错误的
氯
全部的
2015-05-24
即
EN
aig:CAUSC
自动驾驶
2015-06-03
守门员
我需要不带名称空间和soap信封的Xml:

<policySnapshotResponse>
       <requestHeader>
          <id>Spoofy</id>
          <requestApplicationID/>
          <requestMessageID/>
          <echoBack>false</echoBack>
       </requestHeader>
       <applicationContext>
          <businessSegment>CL</businessSegment>
          <region>ALL</region>
          <knowledgeDate>2015-05-24</knowledgeDate>
          <country>IE</country>
          <language>en</language>
          <lineOfBusiness>aig:CAUSC</lineOfBusiness>
          <subLineOfBusiness>AUTOP</subLineOfBusiness>
          <systemDate>2015-06-03</systemDate>
          <targetSystemName>GOALD</targetSystemName>
       </applicationContext>
       <PolicyInqRs>
          <PolInfo>
             <CommlPropertyPolicy>
                <CommlPolicy/>
             </CommlPropertyPolicy>
          </PolInfo>
       </PolicyInqRs>
    </policySnapshotResponse>

惊吓
错误的
氯
全部的
2015-05-24
即
EN
aig:CAUSC
自动驾驶
2015-06-03
守门员
我正在将xml进一步转换为jsonx,然后再转换为json。

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = 
                "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
                   "<soapenv:Body>" +
                      "<ns4:policySnapshotResponse xmlns=\"http://www.aig.com/ACORD1/xml/\" xmlns:ns4=\"http://www.aig.com/gct/services/PolicyInquiryServiceV1.0\" xmlns:ns2=\"http://www.ACORD.org/standards/PC_Surety/ACORD1/xml/\" xmlns:ns3=\"http://www.aig.com/gct/services/commonHeaderV1.0\">" +
                         "<ns4:requestHeader>" +
                            "<ns3:id>Spoofy</ns3:id>" +
                            "<ns3:requestApplicationID/>" +
                            "<ns3:requestMessageID/>" +
                            "<ns3:echoBack>false</ns3:echoBack>" +
                         "</ns4:requestHeader>" +
                         "<ns4:applicationContext>" +
                            "<ns3:businessSegment>CL</ns3:businessSegment>" +
                            "<ns3:region>ALL</ns3:region>" +
                            "<ns3:knowledgeDate>2015-05-24</ns3:knowledgeDate>" +
                            "<ns3:country>IE</ns3:country>" +
                            "<ns3:language>en</ns3:language>" +
                            "<ns3:lineOfBusiness>aig:CAUSC</ns3:lineOfBusiness>" +
                            "<ns3:subLineOfBusiness>AUTOP</ns3:subLineOfBusiness>" +
                            "<ns3:systemDate>2015-06-03</ns3:systemDate>" +
                            "<ns3:targetSystemName>GOALD</ns3:targetSystemName>" +
                         "</ns4:applicationContext>" +
                         "<ns4:PolicyInqRs>" +
                            "<ns2:PolInfo>" +
                               "<ns2:CommlPropertyPolicy>" +
                                  "<ns2:CommlPolicy/>" +
                               "</ns2:CommlPropertyPolicy>" +
                            "</ns2:PolInfo>" +
                         "</ns4:PolicyInqRs>" +
                      "</ns4:policySnapshotResponse>" +
                   "</soapenv:Body>" +
                "</soapenv:Envelope>";

            string pattern = "(</?)([^:]*:)";
            Regex expr = new Regex(pattern);
            input = expr.Replace(input, "$1");

            XDocument doc = XDocument.Parse(input);
            XElement element = doc.Descendants().Where(x => x.Name.LocalName == "policySnapshotResponse").FirstOrDefault();
            foreach (XAttribute attribute in element.Attributes().ToList())
            {
                attribute.Remove();
            }
            doc = XDocument.Parse(element.ToString());

        }
    }
}
​
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
使用System.Text.RegularExpressions;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串输入=
"" +
"" +
"" +
"" +
“惊吓”+
"" +
"" +
“假”+
"" +
"" +
“CL”+
“全部”+
"2015-05-24" +
“即”+
“嗯”+
“aig:CAUSC”+
“自动转账”+
"2015-06-03" +
“进球”+
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"";
string pattern=“(@jdwend

您好,谢谢您的回复。我使用以下方法获得了预期的结果:

下面是我用于将xml转换为jsonx的模板,我在适当的位置将名称更改为本地名称,并获得了所需的输出

<xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>
   <xsl:strip-space elements="*"/>
   <!--Array-->
   <xsl:template match="*[*[2]][local-name(*[1])=local-name(*[2])]">
      <json:object name="{local-name()}">
         <json:array name="{local-name(*[1])}">
            <xsl:apply-templates/>
         </json:array>
      </json:object>
   </xsl:template>
   <!--Array member-->
   <xsl:template match="*[parent::*[ local-name(*[1])=local-name(*[2]) ]] | /">
      <json:object>
         <xsl:apply-templates/>
      </json:object>
   </xsl:template>
   <!--Object-->
   <xsl:template match="*">
      <json:object name="{local-name()}">
         <xsl:apply-templates/>
      </json:object>
   </xsl:template>
   <!--String-->
   <xsl:template match="*[not(*)]">
      <json:string name="{local-name()}">
         <xsl:value-of select="."/>
      </json:string>
   </xsl:template>
</xsl:stylesheet>