XSLT:如何使用XML元素分配常量值
我们有输入XML。在这种情况下,我们尝试使用XSLT:如何使用XML元素分配常量值,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我们有输入XML。在这种情况下,我们尝试使用元素设置常量值 我们基本上希望在每个注释行标记上添加A这个元素常量值。 就是 评论#1 OR-1810143 3. A. 输入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"
元素设置常量值
我们基本上希望在每个注释行标记上添加A
这个元素常量值。
就是
评论#1
OR-1810143
3.
A.
输入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>
人工订单1
A.
9999
abc-1
组装
2
每个
每个
Y
1.
OR-1810141
9999
FBX-SMO30029-8
2014年目标SOAP PDQ组装
3
每个
每个
Y
2.
OR-1810142
评论#1
OR-1810143
3.
评论#2
OR-1810144
4.
我们尝试了以下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>
人工订单1
A.
9999
abc-1
组装
2
每个
每个
Y
1.
OR-1810141
9999
FBX-SMO30029-8
2014年目标SOAP PDQ组装
3
每个
每个
Y
2.
OR-1810142
评论#1
OR-1810143
3.
A.
评论#2
OR-1810144
4.
A.
如果您能提供任何帮助,我们将不胜感激。首先请注意,A
不是有效的XML,因此我假设您指的是A
无论如何,如果所有CommentLine
元素都具有LineActionType
,并且您只想将它们全部设置为a
,那么您可以将此模板与标识模板一起添加
<xsl:template match="CommentLine/LineActionType">
<xsl:copy>A</xsl:copy>
</xsl:template>
首先请注意,A
不是有效的XML,因此我假设您的意思是A
无论如何,如果所有CommentLine
元素都具有LineActionType
,并且您只想将它们全部设置为a
,那么您可以将此模板与标识模板一起添加
<xsl:template match="CommentLine/LineActionType">
<xsl:copy>A</xsl:copy>
</xsl:template>
当我尝试应用两个给定模板时。它对我不起作用。它不会生成语法错误。但也不能创造恒定的价值。看看或看看这两种可能性的作用,这两种可能性都会根据输入产生预期的输出。需要注意的一件事是向XSLT中添加其他也与
CommentLine
匹配的模板,因此您可能需要添加priority
属性。此外,您问题中的XSLT还有一个模板,它可以从库存行
创建评论行
。如果您希望它也包含LineActionType
,只需将其作为现有模板的一部分添加即可。它对我不起作用。它不会生成语法错误。但也不能创造恒定的价值。看看或看看这两种可能性的作用,这两种可能性都会根据输入产生预期的输出。需要注意的一件事是向XSLT中添加其他也与CommentLine
匹配的模板,因此您可能需要添加priority
属性。此外,您问题中的XSLT还有一个模板,它可以从库存行
创建评论行
。如果希望此模板也包含LineActionType
,只需将其作为现有模板的一部分添加即可。是否调试了代码?是否调试了代码?
<xsl:template match="CommentLine">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
<LineActionType>A</LineActionType>
</xsl:copy>
</xsl:template>
<xsl:template match="CommentLine/LineActionType" />