我正在尝试弄清楚如何使用xslt将平面xml文件转换为嵌套的xml文件。 下面是我正在尝试转换的平面xml的示例。我正在使用xslt 2所以我可以使用分组命令,但我不知道如何。
我正在寻找的结果是:
<sources>
<sourcetype>
<sourcetypename>Career Fair</sourcetypename>
<sourcelist>
<source>
<sourcename>Ann Arbor News Fair</sourcename>
<sourceid>CFE-10001</sourceid>
<eventlist>
<event>
<eventname></eventname>
<eventid></eventid>
</event>
</eventlist>
</source>
</sourcelist>
</sourcetype>
</sources>
--- Flat XML ----
<?xml version="1.0" encoding="UTF-8"?>
<ExportXML xmlns:ns1="http://www.taleo.com/ws/integration/toolkit/2005/07" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.taleo.com/ws/integration/toolkit/2005/07" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<record>
<field name="TypeName">Billboards</field>
<field name="SourceName">Billboard</field>
<field name="SourceID">BLD-200284</field>
<field name="EventName"></field>
<field name="EventID"></field>
<field name="State">Active</field>
</record>
<record>
<field name="TypeName">Career Fair</field>
<field name="SourceName">Ann Arbor News Fair</field>
<field name="SourceID">CFE-10001</field>
<field name="EventName"></field>
<field name="EventID"></field>
<field name="State">Active</field>
</record>
<record>
<field name="TypeName">Career Fair</field>
<field name="SourceName">Black Data Processing Association (BDPA) </field>
<field name="SourceID">CFE-10002</field>
<field name="EventName"></field>
<field name="EventID"></field>
<field name="State">Active</field>
</record>
<record>
<field name="TypeName">Career Fair</field>
<field name="SourceName">Career Concepts</field>
<field name="SourceID">CFE-10760</field>
<field name="EventName"></field>
<field name="EventID"></field>
<field name="State">Active</field>
</record>
</ExportXML>
答案 0 :(得分:1)
嵌套一堆for-each-group
元素开始看起来非常讨厌,但它完成了工作:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:t="http://www.taleo.com/ws/integration/toolkit/2005/07">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<sources>
<xsl:for-each-group select="/*/t:record"
group-by="t:field[@name='TypeName']">
<sourcetype>
<sourcetypename>
<xsl:value-of select="current-grouping-key()"/>
</sourcetypename>
<sourcelist>
<xsl:for-each-group select="current-group()"
group-by="t:field[@name='SourceID']">
<source>
<sourcename>
<xsl:value-of select="t:field[@name='SourceName']"/>
</sourcename>
<sourceid>
<xsl:value-of select="current-grouping-key()"/>
</sourceid>
<eventlist>
<xsl:for-each-group
select="current-group()" group-by="t:field[@name='EventID']">
<event>
<eventname>
<xsl:value-of
select="t:field[@name='EventName']"/>
</eventname>
<eventid>
<xsl:value-of
select="current-grouping-key()"/>
</eventid>
</event>
</xsl:for-each-group>
</eventlist>
</source>
</xsl:for-each-group>
</sourcelist>
</sourcetype>
</xsl:for-each-group>
</sources>
</xsl:template>
</xsl:stylesheet>
输出:
<sources xmlns:t="http://www.taleo.com/ws/integration/toolkit/2005/07">
<sourcetype>
<sourcetypename>Billboards</sourcetypename>
<sourcelist>
<source>
<sourcename>Billboard</sourcename>
<sourceid>BLD-200284</sourceid>
<eventlist>
<event>
<eventname/>
<eventid/>
</event>
</eventlist>
</source>
</sourcelist>
</sourcetype>
<sourcetype>
<sourcetypename>Career Fair</sourcetypename>
<sourcelist>
<source>
<sourcename>Ann Arbor News Fair</sourcename>
<sourceid>CFE-10001</sourceid>
<eventlist>
<event>
<eventname/>
<eventid/>
</event>
</eventlist>
</source>
<source>
<sourcename>Black Data Processing Association (BDPA) </sourcename>
<sourceid>CFE-10002</sourceid>
<eventlist>
<event>
<eventname/>
<eventid/>
</event>
</eventlist>
</source>
<source>
<sourcename>Career Concepts</sourcename>
<sourceid>CFE-10760</sourceid>
<eventlist>
<event>
<eventname/>
<eventid/>
</event>
</eventlist>
</source>
</sourcelist>
</sourcetype>
</sources>
答案 1 :(得分:0)
我猜测最终输出是因为我不确定分组应该是什么,但这是另一个2.0分组解决方案:
XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*:ExportXML">
<sources>
<xsl:for-each-group select="*:record" group-by="*:field[@name='TypeName']">
<xsl:variable name="vType" select="*:field[@name='TypeName']"/>
<sourcetype>
<sourcetypename>
<xsl:apply-templates select="*:field[@name='TypeName']" mode="title"/>
</sourcetypename>
<sourcelist>
<xsl:apply-templates select="/*/*:record[*:field[@name='TypeName'][.=$vType]]"/>
</sourcelist>
</sourcetype>
</xsl:for-each-group>
</sources>
</xsl:template>
<xsl:template match="*:field">
<xsl:element name="{lower-case(@name)}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="*:record">
<source>
<xsl:apply-templates select="*:field[starts-with(@name,'Source')]"/>
<eventlist>
<event>
<xsl:apply-templates select="*:field[starts-with(@name,'Event')]"/>
</event>
</eventlist>
</source>
</xsl:template>
<xsl:template match="*:field[@name='TypeName']"/>
<xsl:template match="*:field[@name='TypeName']" mode="title">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
XML输出(使用提供的XML输入和Saxon-HE 9.3)
<sources>
<sourcetype>
<sourcetypename>Billboards</sourcetypename>
<sourcelist>
<source>
<sourcename>Billboard</sourcename>
<sourceid>BLD-200284</sourceid>
<eventlist>
<event>
<eventname/>
<eventid/>
</event>
</eventlist>
</source>
</sourcelist>
</sourcetype>
<sourcetype>
<sourcetypename>Career Fair</sourcetypename>
<sourcelist>
<source>
<sourcename>Ann Arbor News Fair</sourcename>
<sourceid>CFE-10001</sourceid>
<eventlist>
<event>
<eventname/>
<eventid/>
</event>
</eventlist>
</source>
<source>
<sourcename>Black Data Processing Association (BDPA) </sourcename>
<sourceid>CFE-10002</sourceid>
<eventlist>
<event>
<eventname/>
<eventid/>
</event>
</eventlist>
</source>
<source>
<sourcename>Career Concepts</sourcename>
<sourceid>CFE-10760</sourceid>
<eventlist>
<event>
<eventname/>
<eventid/>
</event>
</eventlist>
</source>
</sourcelist>
</sourcetype>
</sources>