将文本文件中的数据转换为 JSON 或 XML
Transform data in Text file to JSON or XML
我是 JSON 的新手,对 XML 知之甚少。我有一个关于从文本到 XML 或 JSON 的数据转换的问题。我已经使用过 XSL 转换,其中我将 XML 文档转换为 XML 或文本。但是现在,我想换一种方式,即 text -> JSON/XML
例如我有以下文字:
One(A,B) One(A',B)
Two(C,D) Two(C',D)
Three(E,F) Three(E',F)
Four(G,H)
Five(I,J)
因此相应的 XML 输出可能如下所示:
<B> A,A' </B>
<D> C,C' </D>
<F> E,E' </F>
<H> G </H>
<J> I </J>
我希望问题很清楚,如果不清楚请告诉我。
提前致谢。
这是 XSLT 3.0 stylesheet you can use with Saxon 9.8 或 Altova XMLSpy/Raptor:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
exclude-result-prefixes="xs math fn"
version="3.0">
<xsl:param name="input-uri" as="xs:string" select="'input1.txt'"/>
<xsl:output indent="yes"/>
<xsl:template name="xsl:initial-template">
<xsl:apply-templates select="unparsed-text-lines($input-uri)"/>
</xsl:template>
<xsl:template match=".[. instance of xs:string]">
<xsl:for-each-group select="analyze-string(., '\w+\(([^,]*),(\w+)\)')//fn:match" group-by="fn:group[@nr = 2]">
<xsl:element name="{current-grouping-key()}">
<xsl:value-of select="current-group()/fn:group[@nr = 1]" separator=","/>
</xsl:element>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
我是 JSON 的新手,对 XML 知之甚少。我有一个关于从文本到 XML 或 JSON 的数据转换的问题。我已经使用过 XSL 转换,其中我将 XML 文档转换为 XML 或文本。但是现在,我想换一种方式,即 text -> JSON/XML
例如我有以下文字:
One(A,B) One(A',B)
Two(C,D) Two(C',D)
Three(E,F) Three(E',F)
Four(G,H)
Five(I,J)
因此相应的 XML 输出可能如下所示:
<B> A,A' </B>
<D> C,C' </D>
<F> E,E' </F>
<H> G </H>
<J> I </J>
我希望问题很清楚,如果不清楚请告诉我。 提前致谢。
这是 XSLT 3.0 stylesheet you can use with Saxon 9.8 或 Altova XMLSpy/Raptor:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
exclude-result-prefixes="xs math fn"
version="3.0">
<xsl:param name="input-uri" as="xs:string" select="'input1.txt'"/>
<xsl:output indent="yes"/>
<xsl:template name="xsl:initial-template">
<xsl:apply-templates select="unparsed-text-lines($input-uri)"/>
</xsl:template>
<xsl:template match=".[. instance of xs:string]">
<xsl:for-each-group select="analyze-string(., '\w+\(([^,]*),(\w+)\)')//fn:match" group-by="fn:group[@nr = 2]">
<xsl:element name="{current-grouping-key()}">
<xsl:value-of select="current-group()/fn:group[@nr = 1]" separator=","/>
</xsl:element>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>