我需要从xml中删除命名空间别名。这个xml是从后端服务接收的(不同的响应可以有不同的结构),最后需要转换为Json格式。 因此,我正在寻找一个通用的XSLT,以便在将其转换为Jsonx然后转换为Json之前从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>
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>
我将xml进一步转换为jsonx,然后转换为json。
答案 0 :(得分:0)
试试这个
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());
}
}
}
答案 1 :(得分:0)
@jdwend
嗨谢谢你的回复。我使用以下方法得到了所需的结果:
下面是我用来将xml转换为jsonx的模板,我在适当的位置将名称更改为local-name并获得所需的输出。
<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>