使用属性作为键(xsl)合并类似的数据xml节点
我想用以下格式转换包含N个节的文件:使用属性作为键(xsl)合并类似的数据xml节点,xml,xslt,xslt-grouping,Xml,Xslt,Xslt Grouping,我想用以下格式转换包含N个节的文件: <File> <Sections Section="Section1" fieldName="field1" fieldValue="value1"/> <Sections Section="Section1" fieldName="field2" fieldValue="value2"/> <Sections Section="Section1" fieldName=
<File>
<Sections Section="Section1" fieldName="field1" fieldValue="value1"/>
<Sections Section="Section1" fieldName="field2" fieldValue="value2"/>
<Sections Section="Section1" fieldName="field3" fieldValue="value3"/>
<Sections Section="Section2" fieldName="field1" fieldValue="value1"/>
<Sections Section="Section2" fieldName="field2" fieldValue="value2"/>
<Sections Section="Section2" fieldName="field3" fieldValue="value3"/>
</File>
进入
使用属性节的值作为键来创建元素
我尝试了一些东西,但我做不到
你能帮我吗
谢谢。这是XSLT 1.0样式表:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:key name="k1" match="File/Sections" use="@Section"/>
<xsl:template match="File">
<xsl:copy>
<xsl:apply-templates select="Sections[generate-id() = generate-id(key('k1', @Section)[1])]" mode="group"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Sections" mode="group">
<xsl:element name="{@Section}">
<xsl:apply-templates select="key('k1', @Section)"/>
</xsl:element>
</xsl:template>
<xsl:template match="Sections">
<xsl:attribute name="{@fieldName}">
<xsl:value-of select="@fieldValue"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
以下是XSLT 1.0样式表:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:key name="k1" match="File/Sections" use="@Section"/>
<xsl:template match="File">
<xsl:copy>
<xsl:apply-templates select="Sections[generate-id() = generate-id(key('k1', @Section)[1])]" mode="group"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Sections" mode="group">
<xsl:element name="{@Section}">
<xsl:apply-templates select="key('k1', @Section)"/>
</xsl:element>
</xsl:template>
<xsl:template match="Sections">
<xsl:attribute name="{@fieldName}">
<xsl:value-of select="@fieldValue"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
下面是XSLT2.0样式表
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/File" >
<xsl:copy>
<xsl:for-each-group select="Sections" group-by="@Section">
<xsl:element name="{current-grouping-key()}">
<xsl:for-each select="current-group()">
<xsl:attribute name="{@fieldName}" select="@fieldValue"/>
</xsl:for-each>
</xsl:element>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
下面是XSLT2.0样式表
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/File" >
<xsl:copy>
<xsl:for-each-group select="Sections" group-by="@Section">
<xsl:element name="{current-grouping-key()}">
<xsl:for-each select="current-group()">
<xsl:attribute name="{@fieldName}" select="@fieldValue"/>
</xsl:for-each>
</xsl:element>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>