我正在尝试将某个XML中的一个元素添加到同一XML中的某些子元素中。有问题的元素已经重复,因此值必须来自XML的正确部分。这是源XML:
<Extract>
<Packet>
<TXREQUESTID>694154</TXREQUESTID>
<Data>
<Property>
<Key>phoneNumber</Key>
</Property>
<Property>
<Key>ownerName</Key>
</Property>
</Data>
<Milestones>
<Milestone>
<Code>123123</Code>
</Milestone>
<Milestone>
<Code>123125</Code>
</Milestone>
</Milestones>
</Packet>
<Packet>
<TXREQUESTID>694155</TXREQUESTID>
<Data>
<Property>
<Key>phoneNumber</Key>
</Property>
<Property>
<Key>ownerName</Key>
</Property>
</Data>
<Milestones>
<Milestone>
<Code>789789</Code>
</Milestone>
<Milestone>
<Code>123126</Code>
</Milestone>
</Milestones>
</Packet>
</Extract>
我需要将TXREQUESTID元素复制为子Property和Milestone元素中的元素。完成后,它需要看起来像这样:
<Extract>
<Packet>
<TXREQUESTID>694154</TXREQUESTID>
<Data>
<Property>
<Key>phoneNumber</Key>
<TXREQUESTID>694154</TXREQUESTID>
</Property>
<Property>
<Key>ownerName</Key>
<TXREQUESTID>694154</TXREQUESTID>
</Property>
</Data>
<Milestones>
<Milestone>
<Code>123123</Code>
<TXREQUESTID>694154</TXREQUESTID>
</Milestone>
<Milestone>
<Code>123125</Code>
<TXREQUESTID>694154</TXREQUESTID>
</Milestone>
</Milestones>
</Packet>
<Packet>
<TXREQUESTID>694155</TXREQUESTID>
<Data>
<Property>
<Key>phoneNumber</Key>
<TXREQUESTID>694155</TXREQUESTID>
</Property>
<Property>
<Key>ownerName</Key>
<TXREQUESTID>694155</TXREQUESTID>
</Property>
</Data>
<Milestones>
<Milestone>
<Code>789789</Code>
<TXREQUESTID>694155</TXREQUESTID>
</Milestone>
<Milestone>
<Code>123126</Code>
<TXREQUESTID>694155</TXREQUESTID>
</Milestone>
</Milestones>
</Packet>
</Extract>
我花了好几个小时没有运气。这似乎应该很简单,但我发现XSLT语法令人费解。有人能指出我正确的方向吗?
答案 0 :(得分:2)
从identity transform开始,并覆盖您要更改的元素(Property
和Milestone
)
XML输入
<Extract>
<Packet>
<TXREQUESTID>694154</TXREQUESTID>
<Data>
<Property>
<Key>phoneNumber</Key>
</Property>
<Property>
<Key>ownerName</Key>
</Property>
</Data>
<Milestones>
<Milestone>
<Code>123123</Code>
</Milestone>
<Milestone>
<Code>123125</Code>
</Milestone>
</Milestones>
</Packet>
<Packet>
<TXREQUESTID>694155</TXREQUESTID>
<Data>
<Property>
<Key>phoneNumber</Key>
</Property>
<Property>
<Key>ownerName</Key>
</Property>
</Data>
<Milestones>
<Milestone>
<Code>789789</Code>
</Milestone>
<Milestone>
<Code>123126</Code>
</Milestone>
</Milestones>
</Packet>
</Extract>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Milestone|Property">
<xsl:copy>
<xsl:apply-templates select="@*|node()|ancestor::Packet/TXREQUESTID"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
XML输出
<Extract>
<Packet>
<TXREQUESTID>694154</TXREQUESTID>
<Data>
<Property>
<TXREQUESTID>694154</TXREQUESTID>
<Key>phoneNumber</Key>
</Property>
<Property>
<TXREQUESTID>694154</TXREQUESTID>
<Key>ownerName</Key>
</Property>
</Data>
<Milestones>
<Milestone>
<TXREQUESTID>694154</TXREQUESTID>
<Code>123123</Code>
</Milestone>
<Milestone>
<TXREQUESTID>694154</TXREQUESTID>
<Code>123125</Code>
</Milestone>
</Milestones>
</Packet>
<Packet>
<TXREQUESTID>694155</TXREQUESTID>
<Data>
<Property>
<TXREQUESTID>694155</TXREQUESTID>
<Key>phoneNumber</Key>
</Property>
<Property>
<TXREQUESTID>694155</TXREQUESTID>
<Key>ownerName</Key>
</Property>
</Data>
<Milestones>
<Milestone>
<TXREQUESTID>694155</TXREQUESTID>
<Code>789789</Code>
</Milestone>
<Milestone>
<TXREQUESTID>694155</TXREQUESTID>
<Code>123126</Code>
</Milestone>
</Milestones>
</Packet>
</Extract>
如果元素顺序很重要,请将xsl:apply-templates
拆分为:
<xsl:apply-templates select="@*|node()"/>
<xsl:apply-templates select="ancestor::Packet/TXREQUESTID"/>