我有一个XML,它在每个订单号下都有一组订单号和一个或多个lineitems。我需要在下面给出的另一个地方有订单号的值。
输入XML
<?xml version="1.0" encoding="utf-8"?>
<orders>
<orderHeader>
<requestDttm>1900-01-01T01:01:01-06:00</requestDttm>
<orderCount>1</orderCount>
</orderHeader>
<orderPayload>
<order>
<createTimestamp>2015-06-11</createTimestamp>
<orderID>ES123456</orderID>
<customer>
<dayPhone>dayPhone1</dayPhone>
<email>email1</email>
<firstName>Test</firstName>
<lastName>Customer1</lastName>
</customer>
<fullfillment>
<taxAmount>3</taxAmount>
<totalPrice>25</totalPrice>
</fullfillment>
<lineItem>
<sku>sku1</sku>
<price>10</price>
<quantity>1</quantity>
</lineItem>
<lineItem>
<sku>sku2</sku>
<price>10</price>
<quantity>2</quantity>
</lineItem>
<lineItem>
<sku>sku3</sku>
<price>5</price>
<quantity>1</quantity>
</lineItem>
<pickupLocation>
<outletID>4804</outletID>
<outletAddress1>outletAddress1</outletAddress1>
<outletAddress2>outletAddress2</outletAddress2>
<city>Texas</city>
<stateCode>TX</stateCode>
<country>US</country>
<postalCode>602</postalCode>
</pickupLocation>
</order>
<order>
<createTimestamp>2015-06-12</createTimestamp>
<orderID>orderID2</orderID>
<customer>
<dayPhone>dayPhone2</dayPhone>
<email>email2</email>
<firstName>Test</firstName>
<lastName>Customer2</lastName>
</customer>
<fullfillment>
<taxAmount>10</taxAmount>
<totalPrice>40</totalPrice>
</fullfillment>
<lineItem>
<sku>sku4</sku>
<price>20</price>
<quantity>1</quantity>
</lineItem>
<lineItem>
<sku>sku5</sku>
<price>10</price>
<quantity>1</quantity>
</lineItem>
<lineItem>
<sku>sku6</sku>
<price>5</price>
<quantity>2</quantity>
</lineItem>
<pickupLocation>
<outletID>outletID2</outletID>
<outletAddress1>outletAddress12</outletAddress1>
<outletAddress2>outletAddress22</outletAddress2>
<city>Topeka</city>
<stateCode>KS</stateCode>
<country>US</country>
<postalCode>601</postalCode>
</pickupLocation>
</order>
<order>
<createTimestamp>2015-06-12</createTimestamp>
<orderID>orderID3</orderID>
<customer>
<dayPhone>dayPhone3</dayPhone>
<email>email3</email>
<firstName>firstName3</firstName>
<lastName>lastName3</lastName>
</customer>
<fullfillment>
<taxAmount>2</taxAmount>
<totalPrice>5</totalPrice>
</fullfillment>
<lineItem>
<sku>sku7</sku>
<price>5</price>
<quantity>1</quantity>
</lineItem>
<pickupLocation>
<outletID>7304</outletID>
<outletAddress1>outletAddress13</outletAddress1>
<outletAddress2>outletAddress23</outletAddress2>
<city>cucta</city>
<stateCode>CU</stateCode>
<country>CO</country>
<postalCode>214</postalCode>
</pickupLocation>
</order>
</orderPayload>
</orders>
输出XML
<?xml version="1.0" encoding="UTF-8"?>
<orders>
<orderHeader>
<requestDttm>1900-01-01T01:01:01-06:00</requestDttm>
<orderCount>1</orderCount>
</orderHeader>
<orderPayload>
<order>
<createTimestamp>2015-06-11</createTimestamp>
<orderID>ES123456</orderID>
<customer>
<dayPhone>dayPhone1</dayPhone>
<email>email1</email>
<firstName>Test</firstName>
<lastName>Customer1</lastName>
</customer>
<fullfillment>
<taxAmount>3</taxAmount>
<totalPrice>25</totalPrice>
</fullfillment>
<lineItem>
<orderNumber>ES123456</orderNumber>
<sku>sku1</sku>
<price>10</price>
<quantity>1</quantity>
</lineItem>
<lineItem>
<orderNumber>ES123456</orderNumber>
<sku>sku2</sku>
<price>10</price>
<quantity>2</quantity>
</lineItem>
<lineItem>
<orderNumber>ES123456</orderNumber>
<sku>sku3</sku>
<price>5</price>
<quantity>1</quantity>
</lineItem>
<pickupLocation>
<outletID>4804</outletID>
<outletAddress1>outletAddress1</outletAddress1>
<outletAddress2>outletAddress2</outletAddress2>
<city>Texas</city>
<stateCode>TX</stateCode>
<country>US</country>
<postalCode>602</postalCode>
</pickupLocation>
</order>
<order>
<createTimestamp>2015-06-12</createTimestamp>
<orderID>orderID2</orderID>
<customer>
<dayPhone>dayPhone2</dayPhone>
<email>email2</email>
<firstName>Test</firstName>
<lastName>Customer2</lastName>
</customer>
<fullfillment>
<taxAmount>10</taxAmount>
<totalPrice>40</totalPrice>
</fullfillment>
<lineItem>
<orderNumber>orderID2</orderNumber>
<sku>sku4</sku>
<price>20</price>
<quantity>1</quantity>
</lineItem>
<lineItem>
<orderNumber>orderID2</orderNumber>
<sku>sku5</sku>
<price>10</price>
<quantity>1</quantity>
</lineItem>
<lineItem>
<orderNumber>orderID2</orderNumber>
<sku>sku6</sku>
<price>5</price>
<quantity>2</quantity>
</lineItem>
<pickupLocation>
<outletID>outletID2</outletID>
<outletAddress1>outletAddress12</outletAddress1>
<outletAddress2>outletAddress22</outletAddress2>
<city>Topeka</city>
<stateCode>KS</stateCode>
<country>US</country>
<postalCode>601</postalCode>
</pickupLocation>
</order>
<order>
<createTimestamp>2015-06-12</createTimestamp>
<orderID>orderID3</orderID>
<customer>
<dayPhone>dayPhone3</dayPhone>
<email>email3</email>
<firstName>firstName3</firstName>
<lastName>lastName3</lastName>
</customer>
<fullfillment>
<taxAmount>2</taxAmount>
<totalPrice>5</totalPrice>
</fullfillment>
<lineItem>
<orderNumber>orderID3</orderNumber>
<sku>sku7</sku>
<price>5</price>
<quantity>1</quantity>
</lineItem>
<pickupLocation>
<outletID>7304</outletID>
<outletAddress1>outletAddress13</outletAddress1>
<outletAddress2>outletAddress23</outletAddress2>
<city>cucta</city>
<stateCode>CU</stateCode>
<country>CO</country>
<postalCode>214</postalCode>
</pickupLocation>
</order>
</orderPayload>
</orders>
答案 0 :(得分:0)
此处不需要for-each循环。首先,您需要一个身份模板:
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
然后,定位要覆盖的节点。在这种情况下,然后是节点lineItem
<xsl:template match="lineItem">
<xsl:copy>
<orderNumber><xsl:value-of select="ancestor::order/orderID"/></orderNumber>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
因此样式表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="lineItem">
<xsl:copy>
<orderNumber><xsl:value-of select="ancestor::order/orderID"/></orderNumber>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>