0
我在這個問題上停留了一段時間。我有這樣一個示例XML文件:使用XSLT轉換XML文件(組合多個模板)
<Records>
<Record><Id>1</Id><Name>table_id</Name><Attribute></Attribute><Value>1</Value></Record>
<Record><Id>1</Id><Name>score</Name><Attribute></Attribute><Value>2.1</Value></Record>
<Record><Id>1</Id><Name>custom4</Name><Attribute>name</Attribute><Value>Custom411</Value></Record>
<Record><Id>1</Id><Name>custom4</Name><Attribute>name</Attribute><Value>Custom412</Value></Record>
<Record><Id>2</Id><Name>table_id</Name><Attribute></Attribute><Value>2</Value></Record>
<Record><Id>2</Id><Name>title</Name><Attribute></Attribute><Value>Title 2</Value></Record>
</Records>
我想所有的ID和行合併到這種格式使用XSLT另一個單獨的XML文件:
<RECORDS>
<RECORD>
<ITEM name="table_id" value="1"/>
<ITEM name="score" value="2.1"/>
<LIST name="custom4">
<LIST_ITEM value="custom411"/>
<LIST_ITEM value="custom412"/>
</LIST>
</RECORD>
<RECORD>
<ITEM name="table_id" value="2"/>
<ITEM name="title" value="Title 2"/>
</RECORD>
</RECORDS
的<table_id> = <Id>
本質價值。因此,對於每個<Id>
,我需要創建一個<RECORD>
。如果一個<Id>
存在多個相同的<Name>
值,那麼我需要爲該<name>
創建一個LIST_ITEM,否則創建一個正常的ITEM。
到目前爲止,我已經能夠使用模板根據<Id>
對記錄進行分組。但我無法使用此模板的輸出生成最終輸出:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="output">
<xsl:call-template name="groupById" />
</xsl:variable>
<OBJECTS>
<xsl:for-each select="$output/Root/Records">
<OBJECT>
<xsl:for-each select="Id/Record">
<ITEM>
<xsl:attribute name='name'>
<xsl:value-of select="Name" />
</xsl:attribute>
<xsl:attribute name='value'>
<xsl:value-of select="Value" />
</xsl:attribute>
</ITEM>
</xsl:for-each>
</OBJECT>
</xsl:for-each>
</OBJECTS>
</xsl:template>
<xsl:template name="groupById">
<Root>
<xsl:for-each-group select="/Records/Record" group-by="Id">
<Records>
<xsl:for-each-group select="current-group()" group-by="if(Id) then Id else 'no key'">
<!-- Copy attributes off the *first* Record element in the group -->
<xsl:copy-of select="current-group()[1]/Id"/>
<!-- Copy remaining children from *all* Record elements in the group -->
<xsl:copy-of select="current-group()"/>
</xsl:for-each-group>
</Records>
</xsl:for-each-group>
</Root>
</xsl:template>
</xsl:stylesheet>
任何人都可以從這裏引導我,或者建議替代路線嗎?
非常感謝你很多,解決方案是優雅的。我完全在另一個軌道上! – Rg90