我一直在檢查 SO 中的相關條目,到目前為止沒有運氣,因為我的用例似乎有一些共性,但也與提供的解決方案有差異(例如:使用 XSLT 將 json 陣列轉換為 xml或使用 XSLT 3.0 函式將 JSON 轉換為 XML)。
所以希望有人可以在這里幫助我。
我這樣稱呼轉變:
java -jar SaxonHE/saxon-he-11.4.jar -s:not_used.xml -xsl:rework_map.xsl -o:report-map.xml p1="list.json"
輸入:list.json - 外部生成的檔案,未嵌入 xml,包含條目陣列,例如:
[
{
"tc_id": "A_S_0001",
"file_name": "\\scripts\\A_S_0001.cs",
"version": "19",
"is_automated": true
},
{
"tc_id": "A_S_0002",
"file_name": "\\scripts\\A_S_0002.cs",
"version": "25",
"is_automated": false
}
]
預期輸出:像這樣:
<list>
<test_case>
<tc_id>A_S_0001</tc_id>
<file_name>\\scripts\\A_S_0001.cs</file_name>
<version>19</version>
<is_automated>true</is_automated>
</test_case>
<test_case>
<tc_id>A_S_0002</tc_id>
<file_name>\\scripts\\A_S_0002.cs</file_name>
<version>25</version>
<is_automated>false</is_automated>
</test_case>
</list>
我的模板 rework_map.xsl(不作業)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:array="http://www.w3.org/2005/xpath-functions/array"
exclude-result-prefixes="xs math map array fn"
version="3.0">
<!-- OUTPUT -->
<xsl:output method="xml" indent="yes"/>
<!-- PARAMETERS -->
<xsl:param name="p1"></xsl:param> <!-- list.json -->
<!-- VARIABLES -->
<xsl:variable name="json-array" select="json-doc($p1)"/>
<xsl:template match="/">
<!-- <xsl:call-template name="common.INFO"/> -->
<list>
<xsl:apply-templates select="$json-array/*"/>
</list>
</xsl:template>
<xsl:template match="fn:map">
<test_case>
<xsl:apply-templates/>
</test_case>
</xsl:template>
<xsl:template match="fn:map/fn:*">
<xsl:element name="{@key}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
uj5u.com熱心網友回復:
-json:list.jsonSaxon 11 的一個選項是使用選項(不要使用選項)提供 JSON 檔案并-s按照以下行撰寫代碼
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:output indent="yes"/>
<xsl:template match=".[. instance of array(map(*))]" name="xsl:initial-template">
<list>
<xsl:apply-templates select="?*"/>
</list>
</xsl:template>
<xsl:template match=".[. instance of map(*)]">
<test_case>
<xsl:variable name="map" select="."/>
<xsl:iterate select="map:keys(.)">
<xsl:element name="{.}">{$map(.)}</xsl:element>
</xsl:iterate>
</test_case>
</xsl:template>
</xsl:stylesheet>
這直接將 JSON 處理為 XPath 3.1 陣列/映射,缺點是 XPath 3.1 映射中的專案沒有順序,因此結果可以是例如
<list>
<test_case>
<version>19</version>
<is_automated>true</is_automated>
<file_name>\scripts\A_S_0001.cs</file_name>
<tc_id>A_S_0001</tc_id>
</test_case>
<test_case>
<version>25</version>
<is_automated>false</is_automated>
<file_name>\scripts\A_S_0002.cs</file_name>
<tc_id>A_S_0002</tc_id>
</test_case>
</list>
另一種選擇是從-itforinitial-template和使用開始,json-to-xml然后只撰寫模板,例如
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:output indent="yes"/>
<xsl:param name="json-string" as="xs:string" select="unparsed-text('list.json'"/>
<xsl:template name="xsl:initial-template">
<list>
<xsl:apply-templates select="json-to-xml($json-string)/*"/>
</list>
</xsl:template>
<xsl:template match="fn:map[not(@key)]">
<test_case>
<xsl:apply-templates/>
</test_case>
</xsl:template>
<xsl:template match="fn:*[@key and not(*)]">
<xsl:element name="{@key}">{.}</xsl:element>
</xsl:template>
</xsl:stylesheet>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/517704.html
上一篇:動態Xml決議TSQL
