我有xml,我希望使用xslt将其输出为json。我发现它的solution,但我需要一些改变,我无法做到这一点因为我是xslt的新手。在这个解决方案中我想要一些嵌套标签作为json中的列表,但是这个解决方案将xml中的所有嵌套标签作为json中的列表给出了我不想要的。重点是我要删除额外的[]。请帮帮我
我的xml是
<?xml version="1.0" encoding="UTF-8"?><User>
<MaritalStatus>Single</MaritalStatus>
<Name>
<FirstName>abc</FirstName>
<MiddleName>def</MiddleName>
<LastName>ghi</LastName>
</Name>
<Relative>
<Father>
<Name>
<FirstName>abc</FirstName>
<MiddleName>def</MiddleName>
<LastName>ghi</LastName>
</Name>
</Father>
<Mother>
<Name>
<FirstName>abc</FirstName>
<MiddleName>def</MiddleName>
<LastName>ghi</LastName>
</Name>
</Mother>
<Sibling>
<Name>
<FirstName>abc</FirstName>
<MiddleName>def</MiddleName>
<LastName>ghi</LastName>
</Name>
</Sibling>
</Relative>
<Friend>
<Name>
<FirstName>abc</FirstName>
<MiddleName>def</MiddleName>
<LastName>ghi</LastName>
</Name>
</Friend></User>
给我的解决方案我提到的是给我json
[{
"MaritalStatus": "Single",
"Name": [
{
"FirstName": "abc",
"MiddleName": "def",
"LastName": "ghi"
}
],
"Relative": [
{
"Father": [
{
"Name": [
{
"FirstName": "abc",
"MiddleName": "def",
"LastName": "ghi"
}
]
}
],
"Mother": [
{
"Name": [
{
"FirstName": "abc",
"MiddleName": "def",
"LastName": "ghi"
}
]
}
],
"Sibling": [
{
"Name": [
{
"FirstName": "ewqrew",
"MiddleName": "defasdfadsf",
"LastName": "ghiqwerwqer"
}
]
}
]
}
],
"Friend": [
{
"Name": [
{
"FirstName": "asd",
"MiddleName": "ghd",
"LastName": "rtu"
}
]
}
]}]
但我想要的输出是
{
"MaritalStatus": "Single",
"Name":
{
"FirstName": "abc",
"MiddleName": "def",
"LastName": "ghi"
},
"Relative": [
{
"Father":
{
"Name":
{
"FirstName": "abc",
"MiddleName": "def",
"LastName": "ghi"
}
},
"Mother":
{
"Name":
{
"FirstName": "abc",
"MiddleName": "def",
"LastName": "ghi"
}
},
"Sibling": [
{
"Name":
{
"FirstName": "ewqrew",
"MiddleName": "defasdfadsf",
"LastName": "ghiqwerwqer"
}
}
]
}
],
"Friend": [
{
"Name":
{
"FirstName": "asd",
"MiddleName": "ghd",
"LastName": "rtu"
}
}
]}
使用Saxon Home您可以利用XPath 3.1功能fn:xml-to-json
。我将提供一个非常简单的解决方案,以便了解这个问题。
下载Saxon Home并在下面创建xslt(test.xsl)。跑
java -jar Saxon-HE-9.7.0-7.jar -s:<your-xml-file> -xsl:test.xsl
(用您下载的版本替换Saxon jar文件名)
test.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="text" encoding="UTF-8" indent="yes" />
<!-- Naive approach -->
<xsl:template match="/">
<xsl:variable name="xmljson">
<xsl:apply-templates />
</xsl:variable>
<xsl:value-of select="fn:xml-to-json($xmljson)" />
</xsl:template>
<xsl:template match="User">
<fn:map>
<xsl:apply-templates />
</fn:map>
</xsl:template>
<xsl:template match="MaritalStatus">
<fn:string key="MaritalStatus">
<xsl:value-of select="." />
</fn:string>
</xsl:template>
<xsl:template match="Name">
<fn:map key="Name">
<xsl:apply-templates />
</fn:map>
</xsl:template>
<xsl:template match="FirstName">
<fn:string key="FirstName">
<xsl:value-of select="." />
</fn:string>
</xsl:template>
<!-- TODO (naive approach)-->
<xsl:template match="Relative" />
<xsl:template match="Friend" />
<xsl:template match="MiddleName" />
<xsl:template match="LastName" />
<!-- TODO better way: fiddle with copy local-name() and more -->
</xsl:stylesheet>
祝好运!