我们有输入XML。我们希望将新行字符的OrderDescription元素拆分为不同的部分,以便它将新的子字符串创建为输出XML中的注释元素。
让我们说字符串数是6。因此,它应该在输出XML中的Stockline元素中创建6个新的注释元素。
是的 对于ex新标签部分应该在每个拆分上创建 <StockLine>
<StockCode></StockCode>
<Comment>Some Comment as split string</comment>
</StockLine>
OrderDetail也是重复节点。让每个与StockCode元素相关的注释为空是很重要的。
输入XML:
<SalesOrders xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="SORTOIDOC.XSD">
<Orders>
<OrderHeader>
<Customer>000016</Customer>
<OrderDate>2016-04-19</OrderDate>
<SalesForceOrderNumber>ORD-411324</SalesForceOrderNumber>
</OrderHeader>
<OrderDetails>
<StockLine>
<StockCode>ABB-CDE-FGH-01</StockCode>
<OrderDescription>EDIORDER-SAVE COMMENTS
C3 Generic
LOC 0833
Expected arrival 01/07/2016
OTYPE NE
TRKPC 01 GM/00007643020008361321</OrderDescription>
<OrderLineID>OR-1561179</OrderLineID>
</StockLine>
</OrderDetails>
</Orders>
</SalesOrders>
我们已应用XSLT AS:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="StockLine">
<xsl:variable name="i" select="position()" />
<xsl:copy>
<xsl:copy-of select="StockCode"/>
<NComment>
<xsl:value-of select="normalize-space(tokenize(../StockLine[1]/OrderDescription, '\n')[$i])"/>
</NComment>
<xsl:copy-of select="OrderLineID"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
预期输出XML:
<SalesOrders xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="SORTOIDOC.XSD">
<Orders>
<OrderHeader>
<Customer>000016</Customer>
<OrderDate>2005-04-19</OrderDate>
<SalesForceOrderNumber>ORD-411324</SalesForceOrderNumber>
</OrderHeader>
<OrderDetails>
<StockLine>
<StockCode></StockCode>
<Comment>EDIORDER-SAVE COMMENTS</comment>
</StockLine>
<StockLine>
<StockCode></StockCode>
<Comment>C3 Generic</comment>
</StockLine>
<StockLine>
<StockCode></StockCode>
<Comment>LOC 0833</comment>
</StockLine>
<StockLine>
<StockCode></StockCode>
<Comment>Expected arrival 01/07/2016</comment>
</StockLine>
<StockLine>
<StockCode></StockCode>
<Comment>OTYPE NE</comment>
</StockLine>
<StockLine>
<StockCode></StockCode>
<Comment>TRKPC 01 GM/00007643020008361321</comment>
</StockLine>
提前致谢!