将多个xml元素转换为单个复杂的xml元素。
我想使用父值将子元素分组到一个复杂的xml中。
在给定的示例中,源xml包含AUDIT_ID=1的两条xml记录。
根据XSLT值,我们是否可以使用AUDIT_ID将目标XML值中给出的两个组合成一个?
源xml:
<Header>
<Audit_records>
<AUDIT_ID>1</AUDIT_ID>
<ESI_ID>100</ESI_ID>
<ESI_NAME>AB</ESI_NAME>
</Audit_records>
<Audit_records>
<AUDIT_ID>1</AUDIT_ID>
<ESI_ID>101</ESI_ID>
<ESI_NAME>BC</ESI_NAME>
</Audit_records>
<Audit_records>
<AUDIT_ID>2</AUDIT_ID>
<ESI_ID>103</ESI_ID>
<ESI_NAME>TH</ESI_NAME>
</Audit_records>
<Audit_records>
<AUDIT_ID>2</AUDIT_ID>
<ESI_ID>104</ESI_ID>
<ESI_NAME>UI</ESI_NAME>
</Audit_records>
</Header>目标Xml:
<Header>
<Audit_records>
<AUDIT_ID>1</AUDIT_ID>
<ESI>
<ESI_ID>100</ESI_ID>
<ESI_NAME>AB</ESI_NAME>
</ESI>
<ESI>
<ESI_ID>101</ESI_ID>
<ESI_NAME>BC</ESI_NAME>
</ESI>
</Audit_records>
<Audit_records>
<AUDIT_ID>2</AUDIT_ID>
<ESI>
<ESI_ID>103</ESI_ID>
<ESI_NAME>TH</ESI_NAME>
</ESI>
<ESI>
<ESI_ID>104</ESI_ID>
<ESI_NAME>UI</ESI_NAME>
</ESI>
</Audit_records>
</Header>使用的xslt:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0"
xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/"
xmlns:ns0="http://xmlns.oracle.com/pcbpel/adapter/db/getrecord"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://xmlns.oracle.com/ATAS_BPEL">
<!--<xsl:key name="myaudit" match="/ns0:getrecordOutputCollection/ns0:getrecordOutput" use="/ns0:getrecordOutputCollection/ns0:getrecordOutput/ns0:AUDIT_ID"/> -->
<xsl:key match="Audit_records" name="myaudit" use="AUDIT_ID"/>
<xsl:template match="/">
<ns1:Header>
<xsl:for-each select="/ns0:Header/ns0:Audit_records[generate-id() = generate-id(key('myaudit',AUDIT_ID)[1])]">
<xsl:sort select="AUDIT_ID" order="ascending" data-type="number" />
<ns1:CARF>
<ns1:Header>
<ns1:AUDIT_ID>
<xsl:value-of select="ns0:AUDIT_ID"/>
</ns1:AUDIT_ID>
<ns1:ESI >
<ns1:ESI_ID>
<xsl:value-of select="key('myaudit',AUDIT_ID)/ns0:ESI_ID"/>
</ns1:ESI_ID>
<ns1:ESI_NAME>
<xsl:value-of select="key('myaudit',AUDIT_ID)/ns0:ESI_NAME"/>
</ns1:ESI_NAME>
</ns1:ESI>
</ns1:Header>
</ns1:CARF>
</xsl:for-each>
</ns1:AUDITRECORDS>
</xsl:template>
</xsl:stylesheet>发布于 2020-03-12 05:16:12
您的XSLT使用了名称空间,但是示例输入/所需的输出没有使用名称空间。
下面是您应该开始使用的内容,但是您必须针对实际数据中的名称空间进行修改……
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="records" match="Audit_records" use="AUDIT_ID"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Header">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:for-each select="Audit_records[generate-id()=generate-id(key('records',AUDIT_ID)[1])]">
<xsl:copy>
<xsl:apply-templates select="@*|AUDIT_ID"/>
<xsl:for-each select="key('records',AUDIT_ID)">
<ESI>
<xsl:apply-templates select="*[not(self::AUDIT_ID)]"/>
</ESI>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>小提琴:http://xsltfiddle.liberty-development.net/bEzknsz/1
https://stackoverflow.com/questions/60622424
复制相似问题