简体   繁体   English

使用XSLT动态生成xml

[英]dynamic xml generation using XSLT

We have incoming JSON data from Source which has data and column in same payload. 我们有来自Source的传入JSON数据,其中的数据和列位于相同的有效负载中。 Using json-to-xml in XSLT I was able to generate XML like following: 在XSLT中使用json-to-xml,我可以生成XML,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<map xmlns="http://www.w3.org/2005/xpath-functions">
   <boolean key="allData">true</boolean>
   <map key="factMap">
      <map key="T!T">
         <array key="rows">
            <map>
               <array key="dataCells">
                  <map>
                     <string key="label">1A</string>
                     <string key="value">1A</string>
                  </map>
                  <map>
                     <string key="label">1B</string>
                     <string key="value">1B</string>
                  </map>
                  <map>
                     <string key="label">1C</string>
                     <string key="value">1C</string>
                  </map>
               </array>
            </map>
            <map>
               <array key="dataCells">
                  <map>
                     <string key="label">2A</string>
                     <string key="value">2A</string>
                  </map>
                  <map>
                     <string key="label">2B</string>
                     <string key="value">2B</string>
                  </map>
                  <map>
                     <string key="label">2C</string>
                     <string key="value">2C</string>
                  </map>
               </array>
            </map>
            <map>
               <array key="dataCells">
                  <map>
                     <string key="label">3A</string>
                     <string key="value">3A</string>
                  </map>
                  <map>
                     <string key="label">3B</string>
                     <string key="value">3B</string>
                  </map>
                  <map>
                     <string key="label">3C</string>
                     <string key="value">3C</string>
                  </map>
               </array>
            </map>
         </array>
      </map>
   </map>
   <map key="detailColumnInfo">
      <map key="Product_vod__c.F1">
         <string key="dataType">string</string>
         <string key="label">F1</string>
      </map>
      <map key="Product_vod__c.F2">
         <string key="dataType">string</string>
         <string key="label">F2</string>
      </map>
      <map key="Product_vod__c.F3">
         <string key="dataType">string</string>
         <string key="label">F3</string>
      </map>
   </map>
</map>

Here each row is available under dataCells tag as label and value. 在这里,每一行都可以在dataCells标签下用作标签和值。 Value under dataCells represents value of field however label does not represent field name. dataCells下的值表示字段的值,但是标签不表示字段名称。 Field names and field order is part of detailColumnInfo tag. 字段名称和字段顺序是detailColumnInfo标记的一部分。 Order and number of fields/data in is not fixed however target XML shall remain fixed. 字段/数据的顺序和数量不固定,但是目标XML应保持固定。 Eg incoming data can have 10 fields and data in any order however target XML shall have only 2 fields. 例如,传入数据可以具有10个字段,并且数据可以按任何顺序排列,但是目标XML应该只有2个字段。 To implement this, I believe, on run time we have to figure out the order of F1 field and have to pick corresponding data from dataCells. 为了实现这一点,我相信在运行时,我们必须弄清楚F1字段的顺序,并且必须从dataCells中选择相应的数据。 Eg if F1 is field is first field in detailColumnInfo then I have to pick first value from all dataCells (1A, 2A, 3A)and generate target XML with value of F1 field and so on. 例如,如果F1是field是detailColumnInfo中的第一个字段,那么我必须从所有dataCells(1A,2A,3A)中选择第一个值,并生成具有F1字段值的目标XML,依此类推。

Detailcolumninfo tag represents the column details. Detailcolumninfo标记表示列详细信息。 Position of value field in Detailcolumninfo will determine the position of fields in Datacells. 值字段在Detailcolumninfo中的位置将确定字段在数据单元格中的位置。 For input code snippet posted in query, if F1 is the 1st label in Detailcolumninfo then first value in Datacells (1A,2A,3A) represents the value of F1. 对于查询中发布的输入代码段,如果F1是Detailcolumninfo中的第一个标签,则数据单元格(1A,2A,3A)中的第一个值表示F1的值。 Similarly 1B,2B,3B represents value of F2. 类似地,1B,2B,3B表示F2的值。 Target XML should look like following: 目标XML应该如下所示:

<Root>   
<Rows> 
<Row> 
<F1> 1A </F1> 
<F2> 1B </F2> 
<F3> 1C </F3> 
</Row> 
<Row> 
<F1>2A </F1> 
<F2> 2B </F2> 
<F3> 2C </F3> 
</Row> 
<Row> 
<F1>3A </F1> 
<F2> 3B </F2> 
<F3> 3C </F3> 
</Row> 
</Rows> 

Can anyone suggest possible solutions to implement such dynamic mapping. 谁能建议实现这种动态映射的可能解决方案。

Here is an example that assumes you simply want to map each array element with key="dataCells" to a Row element and then output its descendants string with key="value" wrapped in the label from the detailColumnInfo: 这是一个示例,假定您只是想将每个带有key="dataCells" array元素映射到Row元素,然后从detailColumnInfo中输出带有包装在标签中的key="value"后代string

<?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:fn="http://www.w3.org/2005/xpath-functions"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    exclude-result-prefixes="xs math fn"
    expand-text="yes"
    version="3.0">

    <xsl:param name="json-xml">
        <map xmlns="http://www.w3.org/2005/xpath-functions">
            <boolean key="allData">true</boolean>
            <map key="factMap">
                <map key="T!T">
                    <array key="rows">
                        <map>
                            <array key="dataCells">
                                <map>
                                    <string key="label">1A</string>
                                    <string key="value">1A</string>
                                </map>
                                <map>
                                    <string key="label">1B</string>
                                    <string key="value">1B</string>
                                </map>
                                <map>
                                    <string key="label">1C</string>
                                    <string key="value">1C</string>
                                </map>
                            </array>
                        </map>
                        <map>
                            <array key="dataCells">
                                <map>
                                    <string key="label">2A</string>
                                    <string key="value">2A</string>
                                </map>
                                <map>
                                    <string key="label">2B</string>
                                    <string key="value">2B</string>
                                </map>
                                <map>
                                    <string key="label">2C</string>
                                    <string key="value">2C</string>
                                </map>
                            </array>
                        </map>
                        <map>
                            <array key="dataCells">
                                <map>
                                    <string key="label">3A</string>
                                    <string key="value">3A</string>
                                </map>
                                <map>
                                    <string key="label">3B</string>
                                    <string key="value">3B</string>
                                </map>
                                <map>
                                    <string key="label">3C</string>
                                    <string key="value">3C</string>
                                </map>
                            </array>
                        </map>
                    </array>
                </map>
            </map>
            <map key="detailColumnInfo">
                <map key="Product_vod__c.F1">
                    <string key="dataType">string</string>
                    <string key="label">F1</string>
                </map>
                <map key="Product_vod__c.F2">
                    <string key="dataType">string</string>
                    <string key="label">F2</string>
                </map>
                <map key="Product_vod__c.F3">
                    <string key="dataType">string</string>
                    <string key="label">F3</string>
                </map>
            </map>
        </map>
    </xsl:param>

    <xsl:variable name="label-names" as="xs:string*" select="$json-xml//fn:map[@key = 'detailColumnInfo']/fn:map/fn:string[@key = 'label']/string()"/>

    <xsl:output indent="yes"/>

    <xsl:template match="/" name="xsl:initial-template">
        <xsl:apply-templates select="$json-xml//fn:map[@key = 'factMap']"/>
    </xsl:template>

    <xsl:template match="fn:map[@key = 'factMap']">
        <Root>
            <Rows>
                <xsl:apply-templates select="$json-xml//fn:array[@key = 'rows']/fn:map/fn:array"/>
            </Rows>
        </Root>
    </xsl:template>

    <xsl:template match="fn:array[@key = 'rows']/fn:map/fn:array">
        <Row>
            <xsl:apply-templates select="fn:map/fn:string[@key = 'value']"/>    
        </Row>
    </xsl:template>

    <xsl:template match="fn:string[@key = 'value']">
        <xsl:element name="{let $pos := position() return $label-names[$pos]}">{.}</xsl:element>
    </xsl:template>

</xsl:stylesheet>

Output is 输出是

<Root>
   <Rows>
      <Row>
         <F1>1A</F1>
         <F2>1B</F2>
         <F3>1C</F3>
      </Row>
      <Row>
         <F1>2A</F1>
         <F2>2B</F2>
         <F3>2C</F3>
      </Row>
      <Row>
         <F1>3A</F1>
         <F2>3B</F2>
         <F3>3C</F3>
      </Row>
   </Rows>
</Root>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM