JSON to XML transformation in XSLT 3.0

只谈情不闲聊 提交于 2019-12-11 06:47:07

问题


I am trying to convert JSON data to XML in XSLT 3.0 using json-to-xml function,but the produced xml is not as expected

for example Input JSON:

{
"glossary": {
    "title": "example glossary",
    "GlossDiv": {
        "title": "S",
        "GlossList": {
            "GlossEntry": {
                "ID": "SGML",
                "SortAs": "SGML",
                "GlossTerm": "Standard Generalized Markup Language",
                "Acronym": "SGML",
                "Abbrev": "ISO 8879:1986",
                "GlossDef": {
                    "para": "A meta-markup language, used to create markup languages such as DocBook.",
                    "GlossSeeAlso": ["GML", "XML"]
                },
                "GlossSee": "markup"
            }
        }
    }
}

}

Generated XML from XSLT3.0:

<?xml version="1.0" encoding="UTF-8"?>
<map xmlns="http://www.w3.org/2005/xpath-functions">
<map key="glossary">
 <string key="title">example glossary</string>
  <map key="GlossDiv">
     <string key="title">S</string>
     <map key="GlossList">
        <map key="GlossEntry">
           <string key="ID">SGML</string>
           <string key="SortAs">SGML</string>
           <string key="GlossTerm">Standard Generalized Markup Language</string>
           <string key="Acronym">SGML</string>
           <string key="Abbrev">ISO 8879:1986</string>
           <map key="GlossDef">
              <string key="para">A meta-markup language, used to create markup languages such as DocBook.</string>
              <array key="GlossSeeAlso">
                 <string>GML</string>
                 <string>XML</string>
              </array>
           </map>
           <string key="GlossSee">markup</string>
        </map>
     </map>
  </map>

Expected XML format:

<glossary>
<title>example glossary</title>
<GlossDiv>
<title>S</title>
<GlossList>
<GlossEntry>
<ID>SGML</ID>
<SortAs>SGML</SortAs>
 <GlossTerm>Standard Generalized Markup Language</GlossTerm>
 <Acronym>SGML</Acronym>
 <Abbrev>ISO 8879:1986</Abbrev>
 <GlossDef>
  <para>A meta-markup language, used to create markuplanguages such as DocBook.</para>
  <GlossSeeAlso OtherTerm="GML">
  <GlossSeeAlso OtherTerm="XML">
 </GlossDef>
 <GlossSee OtherTerm="markup">
</GlossEntry>
</GlossList>
</GlossDiv>
</glossary>

My Major problem here is to ignore these key attributes and print attribute(key) value as element name and actual text as the element text.

can you guys please help me to achieve this.


回答1:


Your "expected" output doesn't seem to be based on any reading of the spec:

https://www.w3.org/TR/xslt-30/#json-to-xml-mapping

The idea is that the XML you are getting captures everything in the JSON losslessly; you can easily transform it to the form you want, and you can then make your own decisions about, for example, handling JSON keys that are not valid XML element names, or when to use elements and when to use attributes.

You can get most of what you want with some simple rules like:

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

<xsl:template match="array">
    <xsl:apply-templates/>
</xsl:template>

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

But some of your rules seem to be particular to the vocabulary, e.g. I can't see why you handle GlossSee differently from title.



来源:https://stackoverflow.com/questions/45633246/json-to-xml-transformation-in-xslt-3-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!