【发布时间】:2019-07-26 10:58:29
【问题描述】:
我有一个带有物料清单的 xml 文件,我必须将其转换为文本文件以进行进一步处理。 xml 文件有一个方法,如果在 Line 节点中 Quantity = 0 且 Component 为空,则 Parent 的值应用于接下来的行,直到找到下一个 Parent 行。在这些组件行中,必须跳过 Parent 的值。
我正在努力使用变量或参数来存储 @Parent 的值以供进一步处理。我发现 xslt 不支持这个 ;-)
谁能告诉我如何处理这个 xml 文件?我可以使用什么方法?我已经阅读了很多带有参数和递归处理的命名模板,但我找不到适合我的案例的正确方法。
我尝试了一个 for-each 循环,并且能够将它存储在一个变量中。
非常简化的输入 XML 示例
<Page>
<Line>
<Quantity>0</Quantity>
<Component></Component>
<Parent>ParentID1</Parent>
</Line>
<Line>
<Quantity>4</Quantity>
<Component>ComponentID1</Component>
<Parent>YYY</Parent>
</Line>
<Line>
<Quantity>2</Quantity>
<Component>ComponentID2</Component>
<Parent>ZZZ</Parent>
</Line>
<Line>
<Quantity>0</Quantity>
<Component></Component>
<Parent>ParentID2</Parent>
</Line>
<Line>
<Quantity>3</Quantity>
<Component>ComponentID4</Component>
<Parent>AAA</Parent>
</Line>
<Line>
<Quantity>2</Quantity>
<Component>ComponentID5</Component>
<Parent>XXX</Parent>
</Line>
<Page>
编辑:答案很有帮助,但真正的 XML 更复杂。我在下面添加了它。我一开始使用的方法是先设置一个变量,但它在 for-each-group 中不起作用。
<xsl:variable name="EPBomQty" >
<xsl:for-each select="Label/Property">
<xsl:if test="PropertyName='Quantity'">
<xsl:value-of select="PropertyValue"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
新的xml
<Page>
<Line>
<Label source_id="1">
<Property FormattingType="0" FormattingLength="32" FormattingRAlign="1">
<PropertyName>Quantity</PropertyName>
<PropertyValue>0</PropertyValue>
</Property>
<Property FormattingType="0" FormattingLength="32" FormattingRAlign="1">
<PropertyName>Component ID</PropertyName>
<PropertyValue/>
</Property>
<Property FormattingType="0" FormattingLength="32" FormattingRAlign="1">
<PropertyName>Parent ID</PropertyName>
<PropertyValue>ParentID1</PropertyValue>
</Property>
</Label>
</Line>
<Line>
<Label source_id="2">
<Property FormattingType="0" FormattingLength="32" FormattingRAlign="1">
<PropertyName>Quantity</PropertyName>
<PropertyValue>4</PropertyValue>
</Property>
<Property FormattingType="0" FormattingLength="32" FormattingRAlign="1">
<PropertyName>Component ID</PropertyName>
<PropertyValue>ComponentID1</PropertyValue>
</Property>
<Property FormattingType="0" FormattingLength="32" FormattingRAlign="1">
<PropertyName>Parent ID</PropertyName>
<PropertyValue>YYY</PropertyValue>
</Property>
</Label>
</Line>
<Line>
<Label source_id="3">
<Property FormattingType="0" FormattingLength="32" FormattingRAlign="1">
<PropertyName>Quantity</PropertyName>
<PropertyValue>2</PropertyValue>
</Property>
<Property FormattingType="0" FormattingLength="32" FormattingRAlign="1">
<PropertyName>Component ID</PropertyName>
<PropertyValue>ComponentID2</PropertyValue>
</Property>
<Property FormattingType="0" FormattingLength="32" FormattingRAlign="1">
<PropertyName>Parent ID</PropertyName>
<PropertyValue>ZZZ</PropertyValue>
</Property>
</Label>
</Line>
<Line>
<Label source_id="4">
<Property FormattingType="0" FormattingLength="32" FormattingRAlign="1">
<PropertyName>Quantity</PropertyName>
<PropertyValue>0</PropertyValue>
</Property>
<Property FormattingType="0" FormattingLength="32" FormattingRAlign="1">
<PropertyName>Component ID</PropertyName>
<PropertyValue/>
</Property>
<Property FormattingType="0" FormattingLength="32" FormattingRAlign="1">
<PropertyName>Parent ID</PropertyName>
<PropertyValue>ParentID2</PropertyValue>
</Property>
</Label>
</Line>
<Line>
<Label source_id="5">
<Property FormattingType="0" FormattingLength="32" FormattingRAlign="1">
<PropertyName>Quantity</PropertyName>
<PropertyValue>3</PropertyValue>
</Property>
<Property FormattingType="0" FormattingLength="32" FormattingRAlign="1">
<PropertyName>Component ID</PropertyName>
<PropertyValue>ComponentID4</PropertyValue>
</Property>
<Property FormattingType="0" FormattingLength="32" FormattingRAlign="1">
<PropertyName>Parent ID</PropertyName>
<PropertyValue>AAA</PropertyValue>
</Property>
</Label>
</Line>
<Line>
<Label source_id="6">
<Property FormattingType="0" FormattingLength="32" FormattingRAlign="1">
<PropertyName>Quantity</PropertyName>
<PropertyValue>2</PropertyValue>
</Property>
<Property FormattingType="0" FormattingLength="32" FormattingRAlign="1">
<PropertyName>Component ID</PropertyName>
<PropertyValue>ComponentID5</PropertyValue>
</Property>
<Property FormattingType="0" FormattingLength="32" FormattingRAlign="1">
<PropertyName>Parent ID</PropertyName>
<PropertyValue>XXX</PropertyValue>
</Property>
</Label>
</Line>
</Page>
预期的输出,例如 xml(可以丢弃标头)
#Parent~Component~Quantity
ParentID1~ComponentID1~4
ParentID1~ComponentID2~2
ParentID2~ComponentID4~3
ParentID2~ComponentID5~2
【问题讨论】:
-
各种答案建议将此视为分组问题,但另一种方法是,在处理每一行时,使用
preceding-sibling::Line[Quantity=0 and Component=''][1]/Parent获取适当的Parent值。 -
@MichaelKay 但问题是如何避免重复查找。