Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用XSLT 3.0实现JSON到XML的转换_Xml_Saxon_Xslt 3.0 - Fatal编程技术网

使用XSLT 3.0实现JSON到XML的转换

使用XSLT 3.0实现JSON到XML的转换,xml,saxon,xslt-3.0,Xml,Saxon,Xslt 3.0,我使用了Windows和saxon9.9(HE) 我的JSON代码是: {“analystId”:“Test”,“jobId”:“profileData”:{“allAuthorCoverage”:false,“abc”:“xyz”,“assetClasses”:[{“status”:“Test1”}]}} 我的XSLT代码是: <xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

我使用了Windowssaxon9.9(HE)

我的JSON代码是:

{“analystId”:“Test”,“jobId”:“profileData”:{“allAuthorCoverage”:false,“abc”:“xyz”,“assetClasses”:[{“status”:“Test1”}]}}

我的XSLT代码是:

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" /> 
<xsl:strip-space elements="*"/>
<xsl:param name="input" select="'simple3.json'"/>
<xsl:template match="@*|node()"> 
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template> 
<xsl:template name="init">
<xsl:apply-templates select="json-to-xml(unparsed-text($input))" mode="copy"/>
</xsl:template>
<xsl:template match="node() | @*" mode="copy">
<xsl:copy>
<xsl:apply-templates select="node() | @*" mode="copy"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

我需要的输出XML是:

<root>
<analystId>Test</analystId>
<jobId></jobId>
<profileData>
<allAuthorCoverage>false</allAuthorCoverage>
<abc>xyz</abc>
</profileData>
</root>

试验
假的
xyz

如何实现这一点?

在XSLT 3.0中,有几种方法可以实现这种转换

一种方法是使用
json-to-xml()
,然后转换生成的xml。生成的XML是

<?xml version="1.0" encoding="UTF-8"?>
<map xmlns="http://www.w3.org/2005/xpath-functions">
   <string key="analystId">Test</string>
   <string key="jobId"/>
   <map key="profileData">
      <boolean key="allAuthorCoverage">false</boolean>
      <string key="abc">xyz</string>
      <array key="assetClasses">
         <map>
            <string key="status">Test1</string>
         </map>
      </array>
   </map>
</map> 

试验
假的
xyz
测试1
您可以使用通用规则(如

<xsl:template match="*[@key]">
  <xsl:element name="{@key}">
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

或使用特定规则,如:

<xsl:template match="fn:string[@key='analystId']>
  <analystId>{.}</analystId>
</xsl:template>

在XSLT 3.0中,有几种方法可以实现这种转换

一种方法是使用
json-to-xml()
,然后转换生成的xml。生成的XML是

<?xml version="1.0" encoding="UTF-8"?>
<map xmlns="http://www.w3.org/2005/xpath-functions">
   <string key="analystId">Test</string>
   <string key="jobId"/>
   <map key="profileData">
      <boolean key="allAuthorCoverage">false</boolean>
      <string key="abc">xyz</string>
      <array key="assetClasses">
         <map>
            <string key="status">Test1</string>
         </map>
      </array>
   </map>
</map> 

试验
假的
xyz
测试1
您可以使用通用规则(如

<xsl:template match="*[@key]">
  <xsl:element name="{@key}">
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

或使用特定规则,如:

<xsl:template match="fn:string[@key='analystId']>
  <analystId>{.}</analystId>
</xsl:template>

@Mihael:除了伪标记外,它工作正常。如何避免伪元素
?我不知道你在做什么,所以我不知道你做错了什么…@Mihael:除了伪标记,它工作正常。如何避免虚拟元素
?我不知道你在做什么,所以我不知道你做错了什么。。。