XSLT:将分组节点移动到父节点



我有以下XML:

<data>
<request method="PUT">
<context>
<record_count>5</record_count>
<record_type>Customer</record_type>
</context>
<value>
<company>ABC</company>
<customer>Contoso</customer>
</value>
<value>
<company>ABC</company>
<customer>Forest</customer>
</value>
<value>
<company>XYZ</company>
<customer>Forest</customer>
</value>
<value>
<company>XYZ</company>
<customer>Cave</customer>
</value>
<value>
<company>ABC</company>
<customer>Cave</customer>
</value>
</request>
</data>

以及以下转换:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="company-key" match="value" use="company"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*" />         
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="/data/request/value" />
<xsl:template match="/data/request/value[count(. | key('company-key', company)[1]) = 1]">
<xsl:element name="group">
<xsl:attribute name="company">
<xsl:value-of select="company"/>
</xsl:attribute>
<xsl:attribute name="method">
<xsl:value-of select="/data/request/@method"/>
</xsl:attribute>
<xsl:attribute name="record-type">
<xsl:value-of select="/data/request/context/record_type"/>
</xsl:attribute>
<xsl:for-each select="key('company-key', company)">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

转换将值节点中的数据分组为新节点。这些节点的名称是group,它们有自己的属性。这工作正常,输出看起来像这样:

<data>
<request method="PUT">
<context>
<record_count>5</record_count>
<record_type>Customer</record_type>
</context>
<group company="ABC" method="PUT" record-type="Customer">
<value>
<company>ABC</company>
<customer>Contoso</customer>
</value>
<value>
<company>ABC</company>
<customer>Forest</customer>
</value>
<value>
<company>ABC</company>
<customer>Cave</customer>
</value>
</group>     
<group company="XYZ" method="PUT" record-type="Customer">
<value>
<company>XYZ</company>
<customer>Forest</customer>
</value>
<value>
<company>XYZ</company>
<customer>Cave</customer>
</value>
</group>       
</request>
</data>

我所面临的问题是,我不能创建一个转换来准备所描述的数据;组";节点到父节点";数据";节点并删除旧的"节点";请求";节点。所有这些都在一个转变中。

输出应该是这样的:

<data>
<group company="ABC" method="PUT" record-type="Customer">
<value>
<company>ABC</company>
<customer>Contoso</customer>
</value>
<value>
<company>ABC</company>
<customer>Forest</customer>
</value>
<value>
<company>ABC</company>
<customer>Cave</customer>
</value>
</group>
<group company="XYZ" method="PUT" record-type="Customer">
<value>
<company>XYZ</company>
<customer>Forest</customer>
</value>
<value>
<company>XYZ</company>
<customer>Cave</customer>
</value>
</group>
</data>

简单地说:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="company-key" match="value" use="company"/>
<xsl:template match="/data">
<xsl:variable name="method" select="request/@method" />
<xsl:variable name="record_type" select="request/context/record_type" />
<data>
<xsl:for-each select="request/value[count(. | key('company-key', company)[1]) = 1]">
<group company="{company}" method="{$method}" record-type="{$record_type}">
<xsl:copy-of select="key('company-key', company)"/>
</group>    
</xsl:for-each>
</data>
</xsl:template>
</xsl:stylesheet>

注意,这假设data只包含一个request

最新更新