【发布时间】:2015-05-26 06:02:52
【问题描述】:
我正在尝试使用 XSLT 定义将 XML 转换为 JSON。我已经得到了所需的输出,但我希望在转换时从我的 json 输出中删除一个根标记,请帮我解决这个问题。
XML 输入
<ArrayOfApiInvoiceReport xmlns="http://schemas.datacontract.org/2004/07/RepSpark.WebServices.Models">
<BillingAddress1>10 N LUMINA AVE</BillingAddress1>
<BillingAddress2/>
<BillingCity>WRIGHTSVILLE BEACH</BillingCity>
<BillingCountry>US</BillingCountry>
<BillingCustomerCode>3003800</BillingCustomerCode>
<BillingCustomerName>SWEETWATER SURF SHOP</BillingCustomerName>
<BillingState>NC</BillingState>
<BillingZip>28480</BillingZip>
<InvoiceAmount>372.72</InvoiceAmount>
<InvoiceCreatedDate>20141110</InvoiceCreatedDate>
<InvoiceItems>
<InvoiceItemSizes>
<InvoiceLineNumber>1</InvoiceLineNumber>
<InvoicedQuantity>3</InvoicedQuantity>
<SizeCode>L</SizeCode>
<UPC>9348282095644</UPC>
</InvoiceItemSizes>
<InvoiceLineNumber>1</InvoiceLineNumber>
<InvoiceNumber>101000005</InvoiceNumber>
<InvoiceItems>
</ArrayOfApiInvoiceReport
使用 XSLT
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes" indent="yes"/>
<!-- Object or Element Property-->
<xsl:template match="*">
<xsl:call-template name="Properties"/>
</xsl:template>
<!-- Array Element -->
<xsl:template match="*" mode="ArrayElement">
<xsl:call-template name="Properties"/>
</xsl:template>
<!-- Object Properties -->
<xsl:template name="Properties">
<xsl:variable name="childName" select="name(*[1])"/>
<xsl:choose>
<xsl:when test="not(*|@*)">"<xsl:value-of select="."/>"</xsl:when>
<xsl:when test="count(*[name()=$childName]) > 1">{ "<xsl:value-of select="$childName"/>" :[<xsl:apply-templates select="*" mode="ArrayElement"/>] }</xsl:when>
<xsl:otherwise>[{
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="*"/>
}]</xsl:otherwise>
</xsl:choose>
<xsl:if test="following-sibling::*">,</xsl:if>
</xsl:template>
<!-- Attribute Property -->
<xsl:template match="@*">"<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>",
</xsl:template>
当前 JSON 输出
"ArrayOfApiInvoiceReport" : [{
"BillingAddress1" : "10 N LUMINA AVE",
"BillingAddress2" : "",
"BillingCity" : "WRIGHTSVILLE BEACH",
"BillingCountry" : "US",
"BillingCustomerCode" : "3003800",
"BillingCustomerName" : "SWEETWATER SURF SHOP",
"BillingState" : "NC",
"BillingZip" : "28480",
"InvoiceAmount" : "372.72",
"InvoiceCreatedDate" : "20141110",
"InvoiceItems" : [{
"InvoiceItemSizes" : [{
"InvoiceLineNumber" : "1",
"InvoicedQuantity" : "3",
"SizeCode" : "L",
"UPC" : "9348282095644"
}],
"InvoiceLineNumber" : "1",
"InvoiceNumber" : "101000005",
"InvoicedQuantity" : "3",
必需的输出是没有"ArrayOfApiInvoiceReport"[{
"BillingAddress1": "10 N LUMINA AVE",
"BillingAddress2": "",
"BillingCity": "WRIGHTSVILLE BEACH",
"BillingCountry": "US",
"BillingCustomerCode": "3003800",
"BillingCustomerName": "SWEETWATER SURF SHOP",
"BillingState": "NC",
"BillingZip": "28480",
"InvoiceAmount": "372.72",
"InvoiceCreatedDate": "20141110",
"InvoiceItems": [
{
"InvoiceItemSizes": [
{
"InvoiceLineNumber": "1",
"InvoicedQuantity": "3",
"SizeCode": "L",
"UPC": "9348282095644"
}
],
"InvoiceLineNumber": "1",
"InvoiceNumber": "101000005",
"InvoicedQuantity": "3",
请帮帮我。
【问题讨论】:
-
所需的输出没有“ArrayOfApiInvoiceReport”。输出应以 [{"BillingAddress1": "10 N ................}] 开头
-
Siva,我对你的问题有几点看法。 XML 无效,因为“InvoiceItems”节点错误关闭;在输出 JSON 上,“InvoiceItems”没有闭包;末尾有一个额外的“InvoicedQuantity”,输入 XML 中没有相应的元素 - 这是计算出来的吗? JSON 以逗号结尾 - 这就是你想要的吗?