【发布时间】:2021-08-12 16:44:26
【问题描述】:
我正在使用下面的通用 xslt 将 xml 转换为 json ,但在第一个数组属性下缺少一些值。期望是使用 make ns1:Value 作为 JSON 数组,即使我们得到单个 xml 元素 在这里,我提到了示例 xml 和 json 消息。 XSLT 代码:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">{
<xsl:apply-templates select="*"/>}
</xsl:template>
<!-- Object or Element Property-->
<xsl:template match="*">
"<xsl:value-of select="name()"/>" : <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="$childName = 'ns1:Value' or 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>
</xsl:stylesheet>
示例 XML:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="urn://opten/soap/cegtar/unique">
<SOAP-ENV:Body>
<ns1:abcResponse>
<ns1:abc id="10485824">
<ns1:Rovatok/>
<ns1:ScoringAdatok>
<ns1:XData tipus="11" megnevezes="APEH hátralék (rendezetlen)">
<ns1:Value num="1" subtype="xbool">false</ns1:Value>
</ns1:XData>
<ns1:XData tipus="13" megnevezes="Egyéb VH (rendezetlen)">
<ns1:Value num="1" subtype="xbool">false</ns1:Value>
</ns1:XData>
<ns1:XData tipus="33" megnevezes="Opten kockázati besorolás">
<ns1:Value num="1" subtype="xstring">A</ns1:Value>
</ns1:XData>
<ns1:XData tipus="46" megnevezes="Cég-állapot (APAFI helyett)">
<ns1:Value num="1" subtype="xdate">0000-00-00</ns1:Value>
<ns1:Value num="2" subtype="xnum">0</ns1:Value>
</ns1:XData>
<ns1:XData tipus="68" megnevezes="Kockázati index szám">
<ns1:Value num="1" subtype="xnum">9</ns1:Value>
</ns1:XData>
<ns1:XData tipus="69" megnevezes="A cég keresdelmi hitelkerete">
<ns1:Value num="1" subtype="xnum">250000</ns1:Value>
<ns1:Value num="2" subtype="xnum">0</ns1:Value>
</ns1:XData>
<ns1:XData tipus="77" megnevezes="Adószám állapot">
<ns1:Value num="1" subtype="xnum">0</ns1:Value>
<ns1:Value num="2" subtype="xstring">Érvényes az adószám</ns1:Value>
</ns1:XData>
</ns1:ScoringAdatok>
<ns1:MerlegAdatok/>
</ns1:abc>
</ns1:abcResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
JSON 响应中缺少属性值,即“tipus 和 megnevezes”值: 预期的 JSON 响应:
{
"SOAP-ENV:Envelope": {
"SOAP-ENV:Body": {
"ns1:MultiInfoResponse": {
"ns1:MultiInfo": {
"id": "10485824",
"ns1:Rovatok": "",
"ns1:ScoringAdatok": {
"ns1:XData": [{
"tipus": "11",
"megnevezes": "APEH hátralék (rendezetlen)",
"ns1:Value": [{
"num": "1",
"subtype": "xbool",
"text": "false"
}]
}, {
"tipus": "13",
"megnevezes": "Egyéb VH (rendezetlen)",
"ns1:Value": [{
"num": "1",
"subtype": "xbool",
"text": "false"
}]
}, {
"tipus": "33",
"megnevezes": "Opten kockázati besorolás",
"ns1:Value": [{
"num": "1",
"subtype": "xstring",
"text": "A"
}]
}, {
"tipus": "46",
"megnevezes": "Cég-állapot (APAFI helyett)",
"ns1:Value": [{
"num": "1",
"subtype": "xdate",
"text": "0000-00-00"
}, {
"num": "2",
"subtype": "xnum",
"text": "0"
}]
}, {
"tipus": "68",
"megnevezes": "Kockázati index szám",
"ns1:Value": [{
"num": "1",
"subtype": "xnum",
"text": "9"
}]
}, {
"tipus": "69",
"megnevezes": "A cég keresdelmi hitelkerete",
"ns1:Value": [{
"num": "1",
"subtype": "xnum",
"text": "250000"
}, {
"num": "2",
"subtype": "xnum",
"text": "0"
}]
}, {
"tipus": "77",
"megnevezes": "Adószám állapot",
"ns1:Value": [{
"num": "1",
"subtype": "xnum",
"text": "0"
}, {
"num": "2",
"subtype": "xstring",
"text": "Érvényes az adószám"
}]
}]
},
"ns1:MerlegAdatok": ""
}
}
}
}
}
【问题讨论】:
-
嗨,我用下面的 xslt 代码进行了测试,但是出现了以下错误。请你建议一下。数组 ( [type] => 2 [message] => XSLTProcessor::transformToXml(): text [file] => /var/www/html/online-toolz.com/functions/XSLT.php [line] => 26 ) 错误:XSLTProcessor::transformToXml(): text
-
我还更新了主票据中的 xml 消息和预期的 JSON 格式。