在应用模板中保留不匹配的节点
Retain unmatched nodes in apply templates
我需要仅在另一个字段 (depositvalue) 不为空时才根据字段(帐户)对记录进行排序。我能够做到这一点,但我当前的 xslt 正在删除应用模板中不匹配的节点。在这种情况下,如何始终保持其他一切不变。要排序的记录 (DirectDeposit) 对于某些员工可能并不总是存在。
我已经在排序上取得了进展。
XSLT:
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output omit-xml-declaration="no" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Employee">
<Employee>
<xsl:apply-templates select="DirectDeposit[not(depositvalue = '')]" >
<xsl:sort select="account"/>
</xsl:apply-templates>
<xsl:apply-templates select="DirectDeposit[depositvalue = '']"/>
</Employee>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
实际结果:(员工header离开了)
<?xml version="1.0"?>
<Hire>
<Employee>
<DirectDeposit>
<action>DirectDeposit</action>
<created_by/>
<created_on_timestamp>2018-07-13T19:46:58.000Z</created_on_timestamp>
<date_of_birth>1985-04-09</date_of_birth>
<account>2</account>
<depositvalue>200</depositvalue>
</DirectDeposit>
<DirectDeposit>
<action>DirectDeposit</action>
<created_by/>
<created_on_timestamp>2018-07-13T19:46:58.000Z</created_on_timestamp>
<date_of_birth>1985-04-09</date_of_birth>
<account>3</account>
<depositvalue>100</depositvalue>
</DirectDeposit>
<DirectDeposit>
<action>DirectDeposit</action>
<created_by/>
<created_on_timestamp>2018-07-13T19:46:58.000Z</created_on_timestamp>
<date_of_birth>1985-04-09</date_of_birth>
<account>1</account>
<depositvalue/>
</DirectDeposit>
</Employee>
<Employee/>
</Hire>
预计:
<?xml version="1.0"?>
<Hire>
<Employee>
<Header>
<action/>
<created_by/>
<created_on_timestamp>2018-07-
13T19:46:58.000Z</created_on_timestamp>
<date_of_birth>1985-04-09</date_of_birth>
<AdditionalElements/>
</Header>
<DirectDeposit>
<action>DirectDeposit</action>
<created_by/>
<created_on_timestamp>2018-07-
13T19:46:58.000Z</created_on_timestamp>
<date_of_birth>1985-04-09</date_of_birth>
<account>2</account>
<depositvalue>200</depositvalue>
</DirectDeposit>
<DirectDeposit>
<action>DirectDeposit</action>
<created_by/>
<created_on_timestamp>2018-07-13T19:46:58.000Z</created_on_timestamp>
<date_of_birth>1985-04-09</date_of_birth>
<account>3</account>
<depositvalue>100</depositvalue>
</DirectDeposit>
<DirectDeposit>
<action>DirectDeposit</action>
<created_by/>
<created_on_timestamp>2018-07-13T19:46:58.000Z</created_on_timestamp>
<date_of_birth>1985-04-09</date_of_birth>
<account>1</account>
<depositvalue/>
</DirectDeposit>
</Employee>
<Employee/>
添加
<xsl:apply-templates select="Header"/>
到您的 match="Employee"
模板。如果您不处理 Header 元素,它就不会被处理,因此不会出现在输出中。
我需要仅在另一个字段 (depositvalue) 不为空时才根据字段(帐户)对记录进行排序。我能够做到这一点,但我当前的 xslt 正在删除应用模板中不匹配的节点。在这种情况下,如何始终保持其他一切不变。要排序的记录 (DirectDeposit) 对于某些员工可能并不总是存在。
我已经在排序上取得了进展。
XSLT:
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output omit-xml-declaration="no" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Employee">
<Employee>
<xsl:apply-templates select="DirectDeposit[not(depositvalue = '')]" >
<xsl:sort select="account"/>
</xsl:apply-templates>
<xsl:apply-templates select="DirectDeposit[depositvalue = '']"/>
</Employee>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
实际结果:(员工header离开了)
<?xml version="1.0"?>
<Hire>
<Employee>
<DirectDeposit>
<action>DirectDeposit</action>
<created_by/>
<created_on_timestamp>2018-07-13T19:46:58.000Z</created_on_timestamp>
<date_of_birth>1985-04-09</date_of_birth>
<account>2</account>
<depositvalue>200</depositvalue>
</DirectDeposit>
<DirectDeposit>
<action>DirectDeposit</action>
<created_by/>
<created_on_timestamp>2018-07-13T19:46:58.000Z</created_on_timestamp>
<date_of_birth>1985-04-09</date_of_birth>
<account>3</account>
<depositvalue>100</depositvalue>
</DirectDeposit>
<DirectDeposit>
<action>DirectDeposit</action>
<created_by/>
<created_on_timestamp>2018-07-13T19:46:58.000Z</created_on_timestamp>
<date_of_birth>1985-04-09</date_of_birth>
<account>1</account>
<depositvalue/>
</DirectDeposit>
</Employee>
<Employee/>
</Hire>
预计:
<?xml version="1.0"?>
<Hire>
<Employee>
<Header>
<action/>
<created_by/>
<created_on_timestamp>2018-07-
13T19:46:58.000Z</created_on_timestamp>
<date_of_birth>1985-04-09</date_of_birth>
<AdditionalElements/>
</Header>
<DirectDeposit>
<action>DirectDeposit</action>
<created_by/>
<created_on_timestamp>2018-07-
13T19:46:58.000Z</created_on_timestamp>
<date_of_birth>1985-04-09</date_of_birth>
<account>2</account>
<depositvalue>200</depositvalue>
</DirectDeposit>
<DirectDeposit>
<action>DirectDeposit</action>
<created_by/>
<created_on_timestamp>2018-07-13T19:46:58.000Z</created_on_timestamp>
<date_of_birth>1985-04-09</date_of_birth>
<account>3</account>
<depositvalue>100</depositvalue>
</DirectDeposit>
<DirectDeposit>
<action>DirectDeposit</action>
<created_by/>
<created_on_timestamp>2018-07-13T19:46:58.000Z</created_on_timestamp>
<date_of_birth>1985-04-09</date_of_birth>
<account>1</account>
<depositvalue/>
</DirectDeposit>
</Employee>
<Employee/>
添加
<xsl:apply-templates select="Header"/>
到您的 match="Employee"
模板。如果您不处理 Header 元素,它就不会被处理,因此不会出现在输出中。