I have a XML
<main>
<DATA_RECORD>
<COMPONENT_SID>100</COMPONENT_SID>
<GROUP_ID>1</GROUP_ID>
</DATA_RECORD>
<DATA_RECORD>
<COMPONENT_SID>200</COMPONENT_SID>
<GROUP_ID>1</GROUP_ID>
</DATA_RECORD>
<DATA_RECORD>
<COMPONENT_SID>400</COMPONENT_SID>
<GROUP_ID>1</GROUP_ID>
</DATA_RECORD>
<DATA_RECORD>
<COMPONENT_SID>10</COMPONENT_SID>
<GROUP_ID>2</GROUP_ID>
</DATA_RECORD>
<DATA_RECORD>
<COMPONENT_SID>20</COMPONENT_SID>
<GROUP_ID>2</GROUP_ID>
</DATA_RECORD>
<DATA_RECORD>
<COMPONENT_SID>2</COMPONENT_SID>
<GROUP_ID>3</GROUP_ID>
</DATA_RECORD>
<DATA_RECORD>
<COMPONENT_SID>4</COMPONENT_SID>
<GROUP_ID>3</GROUP_ID>
</DATA_RECORD>
<DATA_RECORD>
<COMPONENT_SID>8</COMPONENT_SID>
<GROUP_ID>3</GROUP_ID>
</DATA_RECORD>
<DATA_RECORD>
<COMPONENT_SID>16</COMPONENT_SID>
<GROUP_ID>3</GROUP_ID>
</DATA_RECORD>
</main>
I would like to use xsl to parse into another XML. The output would be
<comp value="100,200,400|10,20|2,4,8,16"/>
where the component_sids belonged to different group_id are separated by "|" . The component_sids belonged to the same group id should be concatenated with ",". I used the following xsl
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<x开发者_开发知识库sl:template match="/">
<xsl:element name="comp">
<xsl:attribute name="value">
<xsl:call-template name="join">
<xsl:with-param name="list" select="//DATA_RECORD[GROUP_ID=1]/COMPONENT_SID" />
<xsl:with-param name="separator" select="','" />
</xsl:call-template>
</xsl:attribute>
</xsl:element>
</xsl:template>
<xsl:template name="join">
<xsl:param name="list" />
<xsl:param name="separator"/>
<xsl:for-each select="$list">
<xsl:value-of select="." />
<xsl:if test="position() != last()">
<xsl:value-of select="$separator" />
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The result is
<comp value="100,200,400"/>
But I could not figured out how to separate other groups of component_sid with "|". Can someone help me?
Thanks in advance
The XSLT 2.0 solution can be shortened further to:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="vResult">
<xsl:for-each-group select="*/*" group-by="GROUP_ID">
<xsl:if test="not(position()=1)">|</xsl:if>
<xsl:value-of select="current-group()/COMPONENT_SID" separator=","/>
</xsl:for-each-group>
</xsl:variable>
<comp value="{$vResult}"/>
</xsl:template>
</xsl:stylesheet>
I. XSLT 1.0 solution:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kRecByGrId" match="DATA_RECORD"
use="GROUP_ID"/>
<xsl:variable name="vGrIds" select=
"/*/DATA_RECORD
[generate-id()
=
generate-id(key('kRecByGrId', GROUP_ID)[1])
]
/GROUP_ID
"/>
<xsl:template match="/">
<xsl:variable name="vResult">
<xsl:for-each select="$vGrIds">
<xsl:if test="not(position()=1)">|</xsl:if>
<xsl:apply-templates select=
"key('kRecByGrId', .)/COMPONENT_SID"/>
</xsl:for-each>
</xsl:variable>
<comp value="{$vResult}"/>
</xsl:template>
<xsl:template match="COMPONENT_SID">
<xsl:if test="not(position()=1)">,</xsl:if>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<main>
<DATA_RECORD>
<COMPONENT_SID>100</COMPONENT_SID>
<GROUP_ID>1</GROUP_ID>
</DATA_RECORD>
<DATA_RECORD>
<COMPONENT_SID>200</COMPONENT_SID>
<GROUP_ID>1</GROUP_ID>
</DATA_RECORD>
<DATA_RECORD>
<COMPONENT_SID>400</COMPONENT_SID>
<GROUP_ID>1</GROUP_ID>
</DATA_RECORD>
<DATA_RECORD>
<COMPONENT_SID>10</COMPONENT_SID>
<GROUP_ID>2</GROUP_ID>
</DATA_RECORD>
<DATA_RECORD>
<COMPONENT_SID>20</COMPONENT_SID>
<GROUP_ID>2</GROUP_ID>
</DATA_RECORD>
<DATA_RECORD>
<COMPONENT_SID>2</COMPONENT_SID>
<GROUP_ID>3</GROUP_ID>
</DATA_RECORD>
<DATA_RECORD>
<COMPONENT_SID>4</COMPONENT_SID>
<GROUP_ID>3</GROUP_ID>
</DATA_RECORD>
<DATA_RECORD>
<COMPONENT_SID>8</COMPONENT_SID>
<GROUP_ID>3</GROUP_ID>
</DATA_RECORD>
<DATA_RECORD>
<COMPONENT_SID>16</COMPONENT_SID>
<GROUP_ID>3</GROUP_ID>
</DATA_RECORD>
</main>
produces the wanted, correct result:
<comp value="100,200,400|10,20|2,4,8,16"/>
Explanation:
Muenchian method for grouping -- to find all distinct values of
GROUP_ID
.Simple logic to precede an item (or group) with a delimiter, whenever this item (or group) isn't the first.
II. XSLT 2.0 Solution:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="vResult">
<xsl:for-each-group select="*/*" group-by="GROUP_ID">
<xsl:if test="not(position()=1)">|</xsl:if>
<xsl:for-each select="current-group()/COMPONENT_SID">
<xsl:if test="not(position()=1)">,</xsl:if>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:for-each-group>
</xsl:variable>
<comp value="{$vResult}"/>
</xsl:template>
</xsl:stylesheet>
when applied to the same XML document (above), again the wanted, correct result is produced:
<comp value="100,200,400|10,20|2,4,8,16"/>
Explanation:
Use of
<xsl:for-each-group>
Use of
current-group()
The same simple logic for preceding each item (or group), which isn't the first, with the respective delimiter.
精彩评论