您的 XSLT 存在许多问题。专注于手头的问题,一个问题是您的“订单”模板之一使用了xsl:copy-of
<xsl:template match="order">
<order job_id="{@job_id}" site_code="{@site_code}" replace="{Replace}">
<xsl:apply-templates select="node()"/>
<xsl:copy-of select="../master_version"/>
</order>
</xsl:template>
在继续之前,您有两个匹配“order”的模板。严格来说,这在 XSLT 中被指定为错误。您实际上可能没有看到错误,因为某些处理器会忽略重复的模板,而只使用最后一个。你应该删除第一个。
无论如何,通过使用xsl:copy-of
它只是复制所有master_version
此处的元素,无论此处是否有任何其他模板匹配。你需要使用xsl:apply-templates
<xsl:template match="order">
<order job_id="{@job_id}" site_code="{@site_code}" replace="{Replace}">
<xsl:apply-templates select="node()"/>
<xsl:apply-templates select="../master_version"/>
</order>
</xsl:template>
但这本身是行不通的,因为这两个模板匹配(尽管必须模板都匹配只是一个错误)master_version
如上所述)
<xsl:template match="job_id | site_code | replace | master_version"/>
<xsl:template match="Replace | master_version"/>
去除master_version
从这些模板匹配中,只保留现有的一个
<xsl:template match="master_version[not(ORDER = //order/ORDERPK)]"/>
(请注意,当一个元素与条件匹配时,它实际上会比刚刚匹配的元素具有更高的优先级master_version
,因此在本例中这不是错误)。
此时你也会在那里,但你现在会发现master_version
仍然会在当前位置输出。为了解决这个问题,您可以使用模板匹配dataroot
并添加代码以显式忽略master_version
此时的元素
<xsl:template match="dataroot">
<xsl:copy>
<xsl:apply-templates select="@*|node()[not(self::master_version)]"/>
</xsl:copy>
</xsl:template>
试试这个 XSLT。它可能不会给出您指定的精确输出,但它应该可以解决您的问题master_version
您询问的要素:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="master_version[not(ORDER = //order/ORDERPK)]"/>
<xsl:template match="press_section[not(ORDER = //order/ORDERPK)]"/>
<xsl:template match="version[not(ORDER = //order/ORDERPK)]"/>
<xsl:template match="task_info_press_section[not(ORDER = //order/ORDERPK)]"/>
<xsl:template match="task_info_post_press[not(ORDER = //order/ORDERPK)]"/>
<xsl:template match="post_press_version[not(ORDER = //order/ORDERPK)]"/>
<!-- removes specified nodes from all elements -->
<xsl:template match="ORDER"/>
<xsl:template match="ORDERPK"/>
<xsl:template match="PRESS_x0020_SECTION"/>
<xsl:template match="POST_x0020_PRESS"/>
<!-- Creates attributes against the ORDER element -->
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="dataroot">
<xsl:copy>
<xsl:apply-templates select="@*|node()[not(self::master_version)]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="job_id | site_code | replace | Replace"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="order">
<order job_id="{@job_id}" site_code="{@site_code}" replace="{Replace}">
<xsl:apply-templates select="node()"/>
<xsl:apply-templates select="../master_version"/>
</order>
</xsl:template>
<xsl:template match="task_info_press_section">
<xsl:element name="task_info1">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="task_info_post_press">
<xsl:element name="task_info2">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
另请注意,正如 Daniel Haley 在评论中提到的,多个身份模板已被删除。