问题
We have incoming JSON data from Source which has data and column in same payload. Using json-to-xml in XSLT I was able to generate XML like following:
<?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. Value under dataCells represents value of field however label does not represent field name. Field names and field order is part of detailColumnInfo tag. Order and number of fields/data in is not fixed however target XML shall remain fixed. E.g. incoming data can have 10 fields and data in any order however target XML shall have only 2 fields. 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. E.g. 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.
Detailcolumninfo tag represents the column details. Position of value field in Detailcolumninfo will determine the position of fields in Datacells. 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. Similarly 1B,2B,3B represents value of F2. Target XML should look like following:
<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.
回答1:
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:
<?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>
来源:https://stackoverflow.com/questions/47173669/dynamic-xml-generation-using-xslt