xml到json转换中的空标签



尝试填充空标记而不干扰xsl代码中的现有逻辑。Json文件中只需要显示参数。

需要按照预期的json文件填充空标记。

Input xml file:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:PO_Record
xmlns:ns0="http://Test.com/Order">
<domainId>TEST</domainId>
<hubDomainId>TEST</hubDomainId>
<isForReference>false</isForReference>
<status>Ok</status>
<docStatus>Success</docStatus>
<editingStatus>confirmed</editingStatus>
<vpoNo>10000341</vpoNo>
<vpoDate>2021-03-05</vpoDate>
<instructions></instructions>
<businessRefNo>10000341</businessRefNo>
<totalItems>0</totalItems>
<totalQty>0</totalQty>
<customFields>
<customFieldDate1>2042-01-21</customFieldDate1>
</customFields>
<season>
<code>F22</code>
</season>
<custId>
<custCode>TEST</custCode>
<refNo>002001</refNo>
</custId>
<vendorId>
<vendorCode>1235</vendorCode>
</vendorId>
<vpoItemList></vpoItemList>
<vpoShipDtlDtoGroupList></vpoShipDtlDtoGroupList>    
<vpoShipDtlCsGroupList></vpoShipDtlCsGroupList>
</ns0:PO_Record>

ExpectedJsonFile:
{"domainId":"TEST"hubDomainId":"TEST"isForReference":"false"status":"Ok"docStatus":"Success"editingStatus"信用证上还用写明:","vpoNo":"10000341","vpoDate":"2021 - 03 - 05 -","instructions":","businessRefNo":"10000341","totalItems":"0";"totalQty":"0";"customFields":{"customFieldDate1":"2042 - 01 - 21";},"season":{"code":";F22"},"custId":{"custCode":"TEST"refNo":"002001";},"vendorId":{"vendorCode":"1235";},"vpoItemList":[],"vpoShipDtlDtoGroupList":[],"vpoShipDtlCsGroupList":[]}

xslt code:    
<?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"
exclude-result-prefixes="#all"
xmlns="http://www.w3.org/2005/xpath-functions"
expand-text="yes"
version="3.0">

<xsl:output method="text"/>

<xsl:template match="/">
<xsl:variable name="json-xml">
<xsl:apply-templates/>
</xsl:variable>
<xsl:value-of select="xml-to-json($json-xml, map { 'indent' : true() })"/>
</xsl:template>

<xsl:template match="*[not(*)]">
<string key="{local-name()}">{.}</string>
</xsl:template>

<xsl:template match="*[(*) and . castable as xs:double]">
<number key="{local-name()}">{.}</number>
</xsl:template>

<xsl:template match="*[*]">
<xsl:param name="key" as="xs:boolean" select="false()"/>
<map>
<xsl:if test="$key">
<xsl:attribute name="key" select="local-name()"/>
</xsl:if>
<xsl:for-each-group select="*" group-by="node-name()">
<xsl:choose>
<xsl:when test="current-group()[2] or self::vpoItemList or self::vpoItemCsList or self::vpoShipDtlDtoGroupList or self::vpoShipDtlCsGroupList">
<array key="{local-name()}">
<xsl:choose>
<xsl:when test="self::vpoShipDtlDtoGroupList">
<array>
<xsl:apply-templates select="current-group()">
<xsl:with-param name="key" select="false()"/>
</xsl:apply-templates>                        
</array>
</xsl:when>
<xsl:when test="self::vpoShipDtlCsGroupList">
<xsl:for-each-group select="current-group()" group-by="itemLotNo">
<array>
<xsl:apply-templates select="current-group()">
<xsl:with-param name="key" select="false()"/>
</xsl:apply-templates>
</array>
</xsl:for-each-group>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()">
<xsl:with-param name="key" select="false()"/>
</xsl:apply-templates>
</xsl:otherwise>                      
</xsl:choose>
</array>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()">
<xsl:with-param name="key" select="true()"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</map>
</xsl:template>
</xsl:stylesheet>

也许可以更改最后一个模板来检查空组/元素,例如

<xsl:template match="*[*]">
<xsl:param name="key" as="xs:boolean" select="false()"/>
<map>
<xsl:if test="$key">
<xsl:attribute name="key" select="local-name()"/>
</xsl:if>
<xsl:for-each-group select="*" group-by="node-name()">
<xsl:choose>
<xsl:when test="not(current-group()[2]) and (self::vpoItemList or self::vpoShipDtlDtoGroupList or self::vpoShipDtlCsGroupList)">
<array key="{local-name()}"></array>
</xsl:when>
<xsl:when test="current-group()[2] or self::vpoItemList or self::vpoItemCsList or self::vpoShipDtlDtoGroupList or self::vpoShipDtlCsGroupList">
<array key="{local-name()}">
<xsl:choose>
<xsl:when test="self::vpoShipDtlDtoGroupList">
<array>
<xsl:apply-templates select="current-group()">
<xsl:with-param name="key" select="false()"/>
</xsl:apply-templates>                        
</array>
</xsl:when>
<xsl:when test="self::vpoShipDtlCsGroupList">
<xsl:for-each-group select="current-group()" group-by="itemLotNo">
<array>
<xsl:apply-templates select="current-group()">
<xsl:with-param name="key" select="false()"/>
</xsl:apply-templates>
</array>
</xsl:for-each-group>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()">
<xsl:with-param name="key" select="false()"/>
</xsl:apply-templates>
</xsl:otherwise>                      
</xsl:choose>
</array>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()">
<xsl:with-param name="key" select="true()"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</map>
</xsl:template>

最新更新