[英]Merge two different XML files based on node value using XSLT
我正在尝试使用XSLT合并两个XML文件。 我需要匹配两个文件中都必须存在的tradeId
节点值,然后将所有内容复制到file1
。
任何帮助都将受到欢迎。 所有类似的示例均基于属性和对我不起作用的相同XML。
File1.xml
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<trade>
<tradeId>1</tradeId>
<createdBy>1</createdBy>
<createdOnDate>2</createdOnDate>
<Payment>
<indicator>3</indicator>
<updateBy>4</updateBy>
</Payment>
<Parties>
<partyId>5</partyId>
<partyRoleTypeCode>6</partyRoleTypeCode>
</Parties>
<Product>
<term>7</term>
<shortDescription>8</shortDescription>
</Product>
</trade>
<trade>
<tradeId>2</tradeId>
<createdBy>10</createdBy>
<createdOnDate>20</createdOnDate>
<Payment>
<indicator>30</indicator>
<updateBy>40</updateBy>
</Payment>
<Parties>
<partyId>50</partyId>
<partyRoleTypeCode>60</partyRoleTypeCode>
</Parties>
<Product>
<term>70</term>
<shortDescription>80</shortDescription>
</Product>
</trade>
</S:Body>
</S:Envelope>
File2.xml
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<financialexpectation>
<tradeId>1</tradeId>
<stateCode>TBD</stateCode>
<methodCode>TBD</methodCode>
<TypeCode>NONE</TypeCode>
</financialexpectation>
<financialexpectation>
<tradeId>2</tradeId>
<stateCode>TBD</stateCode>
<methodCode>TBD</methodCode>
<TypeCode>NONE</TypeCode>
</financialexpectation>
<financialexpectation>
<tradeId>3</tradeId>
<stateCode>TBD</stateCode>
<methodCode>TBD</methodCode>
<TypeCode>NONE</TypeCode>
</financialexpectation>
</S:Body>
</S:Envelope>
预期产量
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<trade>
<tradeId>1</tradeId>
<createdBy>1</createdBy>
<createdOnDate>2</createdOnDate>
<Payment>
<indicator>3</indicator>
<updateBy>4</updateBy>
</Payment>
<Parties>
<partyId>5</partyId>
<partyRoleTypeCode>6</partyRoleTypeCode>
</Parties>
<Product>
<term>7</term>
<shortDescription>8</shortDescription>
</Product>
<financialexpectation>
<tradeId>1</tradeId>
<stateCode>TBD</stateCode>
<methodCode>TBD</methodCode>
<TypeCode>NONE</TypeCode>
</financialexpectation>
</trade>
<trade>
<tradeId>2</tradeId>
<createdBy>10</createdBy>
<createdOnDate>20</createdOnDate>
<Payment>
<indicator>30</indicator>
<updateBy>40</updateBy>
</Payment>
<Parties>
<partyId>50</partyId>
<partyRoleTypeCode>60</partyRoleTypeCode>
</Parties>
<Product>
<term>70</term>
<shortDescription>80</shortDescription>
</Product>
<financialexpectation>
<tradeId>2</tradeId>
<stateCode>TBD</stateCode>
<methodCode>TBD</methodCode>
<TypeCode>NONE</TypeCode>
</financialexpectation>
</trade>
</S:Body>
</S:Envelope>
如果将此样式表应用于file1.xml
,它将使用document
函数显式包括来自file2.xml
的相应信息。 不需要在样式表中声明名称空间S
,因为相关元素没有名称空间。 我希望这有帮助。
<?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 method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="trade">
<xsl:copy>
<xsl:apply-templates/>
<xsl:copy-of select="document('file2.xml')//financialexpectation[tradeId=current()/tradeId]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输出
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<trade>
<tradeId>1</tradeId>
<createdBy>1</createdBy>
<createdOnDate>2</createdOnDate>
<Payment>
<indicator>3</indicator>
<updateBy>4</updateBy>
</Payment>
<Parties>
<partyId>5</partyId>
<partyRoleTypeCode>6</partyRoleTypeCode>
</Parties>
<Product>
<term>7</term>
<shortDescription>8</shortDescription>
</Product>
<financialexpectation>
<tradeId>1</tradeId>
<stateCode>TBD</stateCode>
<methodCode>TBD</methodCode>
<TypeCode>NONE</TypeCode>
</financialexpectation>
</trade>
<trade>
<tradeId>2</tradeId>
<createdBy>10</createdBy>
<createdOnDate>20</createdOnDate>
<Payment>
<indicator>30</indicator>
<updateBy>40</updateBy>
</Payment>
<Parties>
<partyId>50</partyId>
<partyRoleTypeCode>60</partyRoleTypeCode>
</Parties>
<Product>
<term>70</term>
<shortDescription>80</shortDescription>
</Product>
<financialexpectation>
<tradeId>2</tradeId>
<stateCode>TBD</stateCode>
<methodCode>TBD</methodCode>
<TypeCode>NONE</TypeCode>
</financialexpectation>
</trade>
</S:Body>
</S:Envelope>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.