我们输入了XML。在那里,我们尝试使用<LineActionType>
元素来设置常量值。
我们基本上希望在每个评论行标记上添加<LineActionType >A<LineActionType />
此元素常量值。
<CommentLine>
<Comment>Comment#1</Comment>
<OrderLineID>OR-1810143</OrderLineID>
<UserDefined>3</UserDefined>
<LineActionType >A<LineActionType />
</CommentLine>
输入XML
<?xml version="1.0" encoding="UTF-8"?>
<SalesOrders xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="SORTOIDOC.XSD">
<Orders>
<OrderHeader>
<CustomerPoNumber>Manual Order 1</CustomerPoNumber>
<OrderActionType>A</OrderActionType>
</OrderHeader>
<OrderDetails>
<StockLine>
<CustomerPoLine>9999</CustomerPoLine>
<StockCode>abc-1</StockCode>
<StockDescription>ASSEMBLED</StockDescription>
<OrderQty>2.0</OrderQty>
<OrderUom>EA</OrderUom>
<PriceUom>EA</PriceUom>
<AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
<UserDefined>1</UserDefined>
<OrderLineID>OR-1810141</OrderLineID>
</StockLine>
<StockLine>
<CustomerPoLine>9999</CustomerPoLine>
<StockCode>FBX-SMO30029-8</StockCode>
<StockDescription>TARGET SOAP PDQ 2014 ASSEMBLED</StockDescription>
<OrderQty>3.0</OrderQty>
<OrderUom>EA</OrderUom>
<PriceUom>EA</PriceUom>
<AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
<UserDefined>2</UserDefined>
<OrderLineID>OR-1810142</OrderLineID>
</StockLine>
<CommentLine>
<Comment>Comment#1</Comment>
<OrderLineID>OR-1810143</OrderLineID>
<UserDefined>3</UserDefined>
<LineActionType />
</CommentLine>
<CommentLine>
<Comment>Comment#2</Comment>
<OrderLineID>OR-1810144</OrderLineID>
<UserDefined>4</UserDefined>
<LineActionType />
</CommentLine>
</OrderDetails>
</Orders>
</SalesOrders>
我们尝试过遵循XSLT 2.0:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
<xsl:output method="xml" encoding="Windows-1252" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="OrderDetails">
<xsl:copy>
<xsl:apply-templates select="*">
<xsl:sort select="xs:integer(UserDefined)" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="StockLine[not(StockCodeDescription) and not (OrderQty) and not(Price)]">
<CommentLine>
<Comment>
<xsl:value-of select="StockCode" />
</Comment>
<xsl:copy-of select="OrderLineID" />
<xsl:copy-of select="UserDefined" />
</CommentLine>
</xsl:template>
<xsl:template match="CommentLine[OrderLineID = preceding-sibling::StockLine/OrderLineID and not(Comment)]" />
<xsl:template match="CommentLine[some $sib in preceding-sibling::CommentLine satisfies deep-equal(., $sib)]" />
</xsl:stylesheet>
预期输出:
<?xml version="1.0" encoding="UTF-8"?>
<SalesOrders xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="SORTOIDOC.XSD">
<Orders>
<OrderHeader>
<CustomerPoNumber>Manual Order 1</CustomerPoNumber>
<OrderActionType>A</OrderActionType>
</OrderHeader>
<OrderDetails>
<StockLine>
<CustomerPoLine>9999</CustomerPoLine>
<StockCode>abc-1</StockCode>
<StockDescription>ASSEMBLED</StockDescription>
<OrderQty>2.0</OrderQty>
<OrderUom>EA</OrderUom>
<PriceUom>EA</PriceUom>
<AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
<UserDefined>1</UserDefined>
<OrderLineID>OR-1810141</OrderLineID>
</StockLine>
<StockLine>
<CustomerPoLine>9999</CustomerPoLine>
<StockCode>FBX-SMO30029-8</StockCode>
<StockDescription>TARGET SOAP PDQ 2014 ASSEMBLED</StockDescription>
<OrderQty>3.0</OrderQty>
<OrderUom>EA</OrderUom>
<PriceUom>EA</PriceUom>
<AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
<UserDefined>2</UserDefined>
<OrderLineID>OR-1810142</OrderLineID>
</StockLine>
<CommentLine>
<Comment>Comment#1</Comment>
<OrderLineID>OR-1810143</OrderLineID>
<UserDefined>3</UserDefined>
<LineActionType >A<LineActionType />
</CommentLine>
<CommentLine>
<Comment>Comment#2</Comment>
<OrderLineID>OR-1810144</OrderLineID>
<UserDefined>4</UserDefined>
<LineActionType >A<LineActionType />
</CommentLine>
</OrderDetails>
</Orders>
</SalesOrders>
对此有任何帮助将不胜感激?
答案 0 :(得分:2)
首先请注意<LineActionType >A<LineActionType />
不是有效的XML,因此我假设您的意思是<LineActionType>A</LineActionType>
。
无论如何,如果所有CommentLine
个元素都有LineActionType
而您只想将它们全部设置为A
,那么您只需将此模板与身份模板一起添加
<xsl:template match="CommentLine/LineActionType">
<xsl:copy>A</xsl:copy>
</xsl:template>
或者,如果LineActionType
下并不总是存在CommentLine
,您可以尝试添加这两个模板。一个是在任何CommentLine
下添加它,另一个是为了防止任何现有的输出(这样它不会输出两次)
<xsl:template match="CommentLine">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
<LineActionType>A</LineActionType>
</xsl:copy>
</xsl:template>
<xsl:template match="CommentLine/LineActionType" />