我必须使用 Oracle Service Bus 在 xslt 1.0 中对 xml 文档进行分组。
这是示例输入文件(简体):
<?xml version="1.0" encoding="UTF-8"?>
<EMailData>
<property name="A">
<property name="B">
<property name="C">
<row>
<property name="C1">
<value>ValC1</value>
</property>
<property name="C2">
<value>ValC2</value>
</property>
<property name="C3">
<value>Valc3</value>
</property>
<property name="C4">
<value>Valc4</value>
</property>
</row>
</property>
<property name="C">
<row>
<property name="C1">
<value>ValC1</value>
</property>
<property name="C2">
<value>ValC2</value>
</property>
<property name="C3">
<value>Valc3</value>
</property>
<property name="C4">
<value>Valc4</value>
</property>
</row>
</property>
<property name="D">
<row>
<property name="D1">
<value>ValD1</value>
</property>
<property name="D2">
<value>VALd2</value>
</property>
<property name="D3-InnerElement"> //Need to Group this too
<row>
<property name="Status">
<value>Status122</value>
</property>
</row>
</property>
<property name="D3-InnerElement">
<row>
<property name="Status">
<value>Status123</value>
</property>
</row>
</property>
<property name="D3-InnerElement">
<row>
<property name="Status">
<value>Status124</value>
</property>
</row>
</property>
</row>
</property>
<property name="D">
<row>
<property name="D1">
<value>ValD1</value>
</property>
<property name="D2">
<value>VALd2</value>
</property>
<property name="D3-InnerElement">
<row>
<property name="Status">
<value>Status122</value>
</property>
</row>
</property>
<property name="D3-InnerElement">
<row>
<property name="Status">
<value>Status123</value>
</property>
</row>
</property>
<property name="D3-InnerElement">
<row>
<property name="Status">
<value>Status124</value>
</property>
</row>
</property>
</row>
</property>
</property>
</property>
</EMailData>
我的 XSLT 逻辑:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:key name="group" match="/*/*/*/property" use="@name"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*/*/*[property[@name]]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:for-each select="*[generate-id() = generate-id(key('group', @name)[1])]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="key('group', @name)/*"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<!--Change for Inner Hierarchy-->
<xsl:key name="inner-group" match="/*/*/*/*/property" use="@name"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*/*/*/*[property[@name]]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:for-each select="*[generate-id() = generate-id(key('inner-group', @name)[1])]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="key('inner-group', @name)/*"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
预期 o/p
<?xml version="1.0" encoding="UTF-8"?>
<EMailData>
<property name="A">
<property name="B">
<property name="C">
<row>
<property name="C1">
<value>ValC1</value>
</property>
<property name="C2">
<value>ValC2</value>
</property>
<property name="C3">
<value>Valc3</value>
</property>
<property name="C4">
<value>Valc4</value>
</property>
</row>
<row>
<property name="C1">
<value>ValC1</value>
</property>
<property name="C2">
<value>ValC2</value>
</property>
<property name="C3">
<value>Valc3</value>
</property>
<property name="C4">
<value>Valc4</value>
</property>
</row>
</property>
<property name="D">
<row>
<property name="D1">
<value>ValD1</value>
</property>
<property name="D2">
<value>VALd2</value>
</property>
<property name="D3-InnerElement"> //Need to Group this too
<row>
<property name="Status">
<value>Status122</value>
</property>
</row>
</property>
<property name="D3-InnerElement">
<row>
<property name="Status">
<value>Status123</value>
</property>
</row>
<row>
<property name="Status">
<value>Status124</value>
</property>
</row>
</property>
</row>
<row>
<property name="D1">
<value>ValD1</value>
</property>
<property name="D2">
<value>VALd2</value>
</property>
<property name="D3-InnerElement">
<row>
<property name="Status">
<value>Status122</value>
</property>
</row>
<row>
<property name="Status">
<value>Status123</value>
</property>
</row>
<row>
<property name="Status">
<value>Status124</value>
</property>
</row>
</property>
</row>
</property>
</property>
</property>
</EMailData>
但是 D3-innerelement 没有分组。告诉我哪里出错了!!
o/p 对于我的 XSLT
<?xml version="1.0" encoding="UTF-8"?>
<EMailData>
<property name="A">
<property name="B">
<property name="C">
<row>
<property name="C1">
<value>ValC1</value>
</property>
<property name="C2">
<value>ValC2</value>
</property>
<property name="C3">
<value>Valc3</value>
</property>
<property name="C4">
<value>Valc4</value>
</property>
</row>
<row>
<property name="C1">
<value>ValC1</value>
</property>
<property name="C2">
<value>ValC2</value>
</property>
<property name="C3">
<value>Valc3</value>
</property>
<property name="C4">
<value>Valc4</value>
</property>
</row>
</property>
<property name="D">
<row>
<property name="D1">
<value>ValD1</value>
</property>
<property name="D2">
<value>VALd2</value>
</property>
<property name="D3-InnerElement"> //Need to Group this too
<row>
<property name="Status">
<value>Status122</value>
</property>
</row>
</property>
<property name="D3-InnerElement">
<row>
<property name="Status">
<value>Status123</value>
</property>
</row>
</property>
<property name="D3-InnerElement">
<row>
<property name="Status">
<value>Status124</value>
</property>
</row>
</property>
</row>
<row>
<property name="D1">
<value>ValD1</value>
</property>
<property name="D2">
<value>VALd2</value>
</property>
<property name="D3-InnerElement">
<row>
<property name="Status">
<value>Status122</value>
</property>
</row>
</property>
<property name="D3-InnerElement">
<row>
<property name="Status">
<value>Status123</value>
</property>
</row>
</property>
<property name="D3-InnerElement">
<row>
<property name="Status">
<value>Status124</value>
</property>
</row>
</property>
</row>
</property>
</property>
</property>
</EMailData>
提前致谢!