关于前缀的信息似乎没有包含在JSON中,因此我决定将它们声明为参数,并假设您知道要开始的元素的名称/键(例如。
Invoice
):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
exclude-result-prefixes="xs math fn"
version="3.0">
<xsl:param name="json-input" as="xs:string">{"_D":"urn:oasis:names:specification:ubl:schema:xsd:Invoice-2",
"_S":"urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2",
"_B":"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2",
"Invoice":[{
"ID":[{"IdentifierContent":"123"}],
"IssueDate":[{"DateContent":"2011-09-22"}],
"InvoicePeriod":[{
"StartDate":[{"DateContent":"2011-08-01"}],
"EndDate":[{"DateContent":"2011-08-31"}]
}],
"AccountingSupplierParty":[{
"Party":[{
"PartyName":[{
"Name":[{"TextContent":"Custom Cotter Pins"}]
}]
}]
}],
"AccountingCustomerParty":[{
"Party":[{
"PartyName":[{
"Name":[{"TextContent":"North American Veeblefetzer"}]
}]
}]
}],
"LegalMonetaryTotal":[{
"PayableAmount":[{"AmountContent":100.00,
"AmountCurrencyIdentifier":"CAD"}]
}],
"InvoiceLine":[{
"ID":[{"IdentifierContent":"1"}],
"LineExtensionAmount":[{"AmountContent":100.00,
"AmountCurrencyIdentifier":"CAD"}],
"Item":[{
"Description":[{"TextContent":"Cotter pin, MIL-SPEC"}]
}]
}]
}]}</xsl:param>
<xsl:param name="prefix2" as="xs:string" select="'cbc'"/>
<xsl:param name="prefix1" as="xs:string" select="'cac'"/>
<xsl:output indent="yes"/>
<xsl:template name="xsl:initial-template">
<xsl:apply-templates select="json-to-xml($json-input)//fn:array[@key = 'Invoice']"/>
</xsl:template>
<xsl:template match="fn:array[@key = 'Invoice']" priority="5">
<xsl:variable name="ns" select="../fn:string[@key = '_D']"/>
<xsl:variable name="ns1" select="../fn:string[@key = '_S']"/>
<xsl:variable name="ns2" select="../fn:string[@key = '_B']"/>
<xsl:element name="{@key}" namespace="{$ns}">
<xsl:namespace name="{$prefix1}" select="$ns1"/>
<xsl:namespace name="{$prefix2}" select="$ns2"/>
<xsl:apply-templates>
<xsl:with-param name="ns1" select="$ns1" tunnel="yes"/>
<xsl:with-param name="ns2" select="$ns2" tunnel="yes"/>
</xsl:apply-templates>
</xsl:element>
</xsl:template>
<xsl:template match="fn:array[@key and *[1][self::fn:map[fn:string]] and not(*[2])]">
<xsl:param name="ns2" tunnel="yes"/>
<xsl:element name="{$prefix2}:{@key}" namespace="{$ns2}">
<xsl:value-of select="fn:map/fn:string"/>
</xsl:element>
</xsl:template>
<xsl:template match="fn:array[@key and fn:map[fn:array]]">
<xsl:param name="ns1" tunnel="yes"/>
<xsl:element name="{$prefix1}:{@key}" namespace="{$ns1}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>