[英]xslt to add existing node in xml to another node based on a particular value using for each loop
我有一個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>
不需要在此循環。 首先,您需要具有一個身份模板:
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.