使用XSLT 3.0实现JSON到XML的转换
我使用了Windows和saxon9.9(HE) 我的JSON代码是:使用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">
{“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:除了伪标记,它工作正常。如何避免虚拟元素
和
?我不知道你在做什么,所以我不知道你做错了什么。。。