1
我想組和樞紐輸入XML數據,並轉換成JSON,我有大部分工作,除了我認爲是dymanic列名 這是我目前XSLTXSLT樞軸動態列
<?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" version="2.0">
<xsl:output method="text" encoding="utf-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="root">
<xsl:text>{"root":{</xsl:text>
<xsl:text>"Table":[</xsl:text>
<xsl:for-each-group select="Metric[@TableName != '']" group-by="@measObjLdn">
<xsl:sort select="current-grouping-key()"/>
<xsl:variable name="curr_key" select="current-grouping-key()"/>
<xsl:for-each-group select="current-group()[@TableName!='']" group-by="@TableName">
<xsl:sort select="current-grouping-key()"/>
<xsl:text>{"TableName":"</xsl:text>
<xsl:value-of select="current-grouping-key()"/>
<xsl:text>",</xsl:text>
<xsl:text>"Stamp":"</xsl:text>
<xsl:value-of select="@BeginTime"/>
<xsl:text>",</xsl:text>
<xsl:text>"measObjLdn":"</xsl:text>
<xsl:value-of select="$curr_key"/>
<xsl:text>",</xsl:text>
<xsl:text>"Element_Type":"</xsl:text>
<xsl:value-of select="@Element_Type"/>
<xsl:text>",</xsl:text>
<xsl:text>"Key1":"</xsl:text>
<xsl:value-of select="@Key1"/>
<xsl:text>"</xsl:text>
<xsl:for-each select="current-group()">
<xsl:for-each select="@ColumnName">
<xsl:text>,"</xsl:text>
<xsl:value-of select="."/>
<xsl:text>":"</xsl:text>
<xsl:value-of select="@measResult"/>
<xsl:text>"</xsl:text>
</xsl:for-each>
</xsl:for-each>
<xsl:text>}</xsl:text>
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each-group>
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each-group>
<xsl:text>]}}</xsl:text>
</xsl:template>
</xsl:stylesheet>
以下是文件
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xs="http://www.w3.org/2001/XMLSchema">
<Metric measType="50332573"
infoid="50331655"
measResult="768"
h50332573="768"
BeginTime="2016-12-29T23:30:00-07:00"
measObjLdn="THAB23626/ULGROUP:UL BB Resource Group No.=0"
Element_Type="ULGROUP"
Key1="THAB23626/ULGROUP:=0"
TableName="HH_CORE_BASE"
ColumnName="VSCEULAVAILABLEULGROUP"
VSCEULAVAILABLEULGROUP="768"/>
<Metric measType="50332574"
infoid="50331655"
measResult="14.65"
h50332574="14.65"
BeginTime="2016-12-29T23:30:00-07:00"
measObjLdn="THAB23626/ULGROUP:UL BB Resource Group No.=0"
Element_Type="ULGROUP"
Key1="THAB23626/ULGROUP:=0"
TableName="HH_CORE_BASE"
ColumnName="VSCEULMEANULGROUP"
VSCEULMEANULGROUP="14.65"/>
<Metric measType="50342574"
infoid="50331655"
measResult="8.5"
h50342574="8.5"
BeginTime="2016-12-29T23:30:00-07:00"
measObjLdn="THAB23626/ULGROUP:UL BB Resource Group No.=0"
Element_Type="ULGROUP"
Key1="THAB23626/ULGROUP:=0"
TableName="HH_CORE_BASE"
ColumnName="VSCEULMINULGROUP"
VSCEULMINULGROUP="8.5"/>
<Metric measType="50342575"
infoid="50331655"
measResult="23.5"
h50342575="23.5"
BeginTime="2016-12-29T23:30:00-07:00"
measObjLdn="THAB23626/ULGROUP:UL BB Resource Group No.=0"
Element_Type="ULGROUP"
Key1="THAB23626/ULGROUP:=0"
TableName="HH_CORE_BASE"
ColumnName="VSCEULMAXULGROUP"
VSCEULMAXULGROUP="23.5"/>
<Metric measType="50342635"
infoid="50331655"
measResult="0"
h50342635="0"
BeginTime="2016-12-29T23:30:00-07:00"
measObjLdn="THAB23626/ULGROUP:UL BB Resource Group No.=0"
Element_Type="ULGROUP"
Key1="THAB23626/ULGROUP:=0"
TableName=""/>
</root>
從XSLT的輸出是如下的樣本,但是我錯過從列即「VSCEULMEANULGROUP」值:「14.65」,是它使用易於編碼靜態ColumnName和measResult(不適用於我)或「VSCEULME ANULGROUP 「=」 14.65" 從XML
{
"root":{
"Table":[{
"TableName":"HH_CORE_BASE",
"Stamp":"2016-12-29T23:30:00-07:00",
"measObjLdn":"THAB23626/ULGROUP:UL BB Resource Group No.=0",
"Element_Type":"ULGROUP",
"Key1":"THAB23626/ULGROUP:=0",
"VSCEULAVAILABLEULGROUP":"",
"VSCEULMEANULGROUP":"",
"VSCEULMINULGROUP":"",
"VSCEULMAXULGROUP":""
}
]
}
}
如果您希望更多人研究該問題,請考慮簡化它。當問題可以用兩個名爲foo和bar的屬性很好地說明時,我們不必通過名稱爲VSCEULAVAILABLEULGROUP的20個屬性。 –