方形支架,适用于单个和;xml到json中的多个行项
如何获取单个或多个“详细信息”行项目的方括号或数组。 需要获得方括号或数组[即使对于单行'Details'也是如此。我们有多行'Details'。 要求是填充[甚至从单行“详细信息”填充] 单行“详细信息”的xmlFile:方形支架,适用于单个和;xml到json中的多个行项,json,xslt,xslt-2.0,xslt-grouping,xslt-3.0,Json,Xslt,Xslt 2.0,Xslt Grouping,Xslt 3.0,如何获取单个或多个“详细信息”行项目的方括号或数组。 需要获得方括号或数组[即使对于单行'Details'也是如此。我们有多行'Details'。 要求是填充[甚至从单行“详细信息”填充] 单行“详细信息”的xmlFile: <root> <FirstName>Alex</FirstName> <LastName>Fin</LastName> <Details> <Id_Numb
<root>
<FirstName>Alex</FirstName>
<LastName>Fin</LastName>
<Details>
<Id_Number>111</Id_Number>
<Location>NC</Location>
<Contact>
<PhoneNumber>+1 323</PhoneNumber>
</Contact>
</Details>
</root>
包含多行“详细信息”的xml文件:
<root>
<FirstName>Alex</FirstName>
<LastName>Fin</LastName>
<Details>
<Id_Number>111</Id_Number>
<Location>NC</Location>
<Contact>
<PhoneNumber>+1 323</PhoneNumber>
</Contact>
</Details>
<Details>
<Id_Number>222</Id_Number>
<Location>TX</Location>
<Contact>
<PhoneNumber>+1 323</PhoneNumber>
</Contact>
</Details>
<Address>
<Locality>Urban</Locality>
<Distance>
<Miles>2</Miles>
</Distance>
<Commute>Yes</Commute>
</Address>
<Address>
<Locality>Rular</Locality>
<Distance>
<Miles>1</Miles>
</Distance>
<Commute>Yes</Commute>
</Address>
</root>
XSLT代码:
<?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"
exclude-result-prefixes="#all"
xmlns="http://www.w3.org/2005/xpath-functions"
expand-text="yes"
version="3.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:variable name="json-xml">
<xsl:apply-templates/>
</xsl:variable>
<xsl:value-of select="xml-to-json($json-xml, map { 'indent' : true() })"/>
</xsl:template>
<xsl:template match="*[not(*)]">
<string key="{local-name()}">{.}</string>
</xsl:template>
<xsl:template match="*[(*) and . castable as xs:double]">
<number key="{local-name()}">{.}</number>
</xsl:template>
<xsl:template match="*[*]">
<xsl:param name="key" as="xs:boolean" select="false()"/>
<map>
<xsl:if test="$key">
<xsl:attribute name="key" select="local-name()"/>
</xsl:if>
<xsl:for-each-group select="*" group-by="node-name()">
<xsl:choose>
<xsl:when test="current-group()[2] or self::Details or self::Contact">
<array key="{local-name()}">
<xsl:choose>
<xsl:when test="self::Address">
<array>
<xsl:apply-templates select="current-group()">
<xsl:with-param name="key" select="false()"/>
</xsl:apply-templates>
</array>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()">
<xsl:with-param name="key" select="false()"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</array>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()">
<xsl:with-param name="key" select="true()"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</map>
</xsl:template>
</xsl:stylesheet>
{.}
{.}
当相似命名节点组包含两个或更多项时,代码将生成一个数组:
<xsl:when test="current-group()[2]">
<array key="{local-name()}">
<xsl:apply-templates select="current-group()"/>
</array>
</xsl:when>
因此,最简单的修复方法是在元素名为“Details”时使用相同的逻辑:
或者更好的设计可能是使其数据驱动;使用一个全局变量,其中包含要强制数组输出的元素名称列表,并测试元素在此列表中的成员身份。非常感谢Michael-如何将地址行项作为数组“Address”:[[{“Location”:“Urban”}],[{“地点”:“Rular”}]]请不要问新问题作为对现有问题的补充。当你问问题时,请解释你尝试了什么以及你遇到了什么困难,否则就好像你想让别人为你做你的工作。我尝试了模板,但子元素没有填充“距离”在json-“Address section”中。子元素无法识别。要从“Address”}获取子元素,请执行任何usggestions操作,非常感谢您的支持…我修改了代码…它适用于单个或多个行项目和子元素
<?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"
exclude-result-prefixes="#all"
xmlns="http://www.w3.org/2005/xpath-functions"
expand-text="yes"
version="3.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:variable name="json-xml">
<xsl:apply-templates/>
</xsl:variable>
<xsl:value-of select="xml-to-json($json-xml, map { 'indent' : true() })"/>
</xsl:template>
<xsl:template match="*[not(*)]">
<string key="{local-name()}">{.}</string>
</xsl:template>
<xsl:template match="*[(*) and . castable as xs:double]">
<number key="{local-name()}">{.}</number>
</xsl:template>
<xsl:template match="*[*]">
<xsl:param name="key" as="xs:boolean" select="false()"/>
<map>
<xsl:if test="$key">
<xsl:attribute name="key" select="local-name()"/>
</xsl:if>
<xsl:for-each-group select="*" group-by="node-name()">
<xsl:choose>
<xsl:when test="current-group()[2] or self::Details or self::Contact">
<array key="{local-name()}">
<xsl:choose>
<xsl:when test="self::Address">
<array>
<xsl:apply-templates select="current-group()">
<xsl:with-param name="key" select="false()"/>
</xsl:apply-templates>
</array>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()">
<xsl:with-param name="key" select="false()"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</array>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()">
<xsl:with-param name="key" select="true()"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</map>
</xsl:template>
</xsl:stylesheet>
<xsl:when test="current-group()[2]">
<array key="{local-name()}">
<xsl:apply-templates select="current-group()"/>
</array>
</xsl:when>
<xsl:when test="current-group()[2] or self::Details">