在这个代码示例中,我有两个挑战,一个是在 B 节点之后和节点 C、D 和 E 之前设置 B1 节点,第二个挑战是将第二个 KEY 节点添加到 /ROOT/E/OTHER/DEAL/KEYS 结构。
此 XML 示例:
<ROOT>
<A>some A text</A>
<B>some B text</B>
<C>some C text</C>
<D>some D text</D>
<E>
<OTHER>
<DEAL>
<KEYS>
<KEY>
<KeyIdentifierType>KeyIdentifierTypeA</KeyIdentifierType>
<KeyValue>123456|1</KeyValue>
</KEY>
</KEYS>
</DEAL>
</OTHER>
</E>
</ROOT>
改造后:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Identifiers are added by the system. Need to pass parms from the calling program -->
<xsl:template match="ROOT" name="add-B1">
<xsl:variable name="elements-after" select="C|D|E"/>
<xsl:copy>
<xsl:copy-of select="* except $elements-after"/>
<B1>some B1 text</B1>
<xsl:copy-of select="$elements-after"/>
</xsl:copy>
</xsl:template>
<!-- KEY is added by the system. Need to pass parms from the calling program -->
<xsl:template match="ROOT/E/OTHER/DEAL/KEYS" name="add-KEYS">
<xsl:param name="KeyIdentifierTypeB">654321|1</xsl:param>
<xsl:copy>
<xsl:copy-of select="*"/>
<KEY>
<KeyIdentifierType>KeyIdentifierTypeB</KeyIdentifierType>
<KeyValue>
<xsl:value-of select="$KeyIdentifierTypeB"/>
</KeyValue>
</KEY>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
变得:
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
<A>some A text</A>
<B>some B text</B>
<B1 xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">some B1 text</B1>
<C>some C text</C>
<D>some D text</D>
<E>
<OTHER>
<DEAL>
<KEYS>
<KEY>
<KeyIdentifierType>KeyIdentifierTypeA</KeyIdentifierType>
<KeyValue>123456|1</KeyValue>
</KEY>
</KEYS>
</DEAL>
</OTHER>
</E>
</ROOT>
为什么第二个模板定义被完全忽略了?
第一个代码学院已解决 B1 节点设置在 B 节点之后和节点 C、D 和 E 之前,或者换句话说 B1 节点已设置并且必须在其后放置的节点是:C、D 和 E。
第二个模板match="ROOT/E/OTHER/DEAL/KEYS" 应该满足第二个挑战部分:将第二个 KEY 节点添加到 /ROOT/E/OTHER/DEAL/KEYS 结构,已被完全忽略。除了这个事实,如果你在 ROOT 节点上注释第一个模板匹配,第二个模板 match="ROOT/E/OTHER/DEAL/KEYS" 将正常工作,它实际上会添加额外的密钥,但我没有不知道为什么第一个模板匹配总是覆盖第二个。我尝试 xsl:template match="ROOT/E/OTHER/DEAL/KEYS... 和 xsl:for-each select=... 和 xsl:call-template name="add-KEYS"
我实际上理解,应用模板匹配具有最高优先级的更高结构的节点模板。更改 XSLT 文件中的模板位置不会有任何影响,它不会按照确切的行顺序读取它,而是会通过匹配优先级来处理它。每个匹配模板的“apply-templates”都会对 XML 结构进行更改,隐含地它会为我们生成“for-each”,但我不知道如何“建议”第二个模板结构已更改,以及为什么我应该这样做,因为第二个模板匹配正在寻找不同的 XPath 位置,顺便说一句,从第一个模板中从未触及过。我应该在我的情况下应用模板排序吗?......以及如何做到这一点的最佳实践是什么?
预期结果:
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
<A>some A text</A>
<B>some B text</B>
<B1>some B1 text</B1>
<C>some C text</C>
<D>some D text</D>
<E>
<OTHER>
<DEAL>
<KEYS>
<KEY>
<KeyIdentifierType>KeyIdentifierTypeA</KeyIdentifierType>
<KeyValue>123456|1</KeyValue>
</KEY>
<KEY>
<KeyIdentifierType>KeyIdentifierTypeB</KeyIdentifierType>
<KeyValue>654321|1</KeyValue>
</KEY>
</KEYS>
</DEAL>
</OTHER>
</E>
</ROOT>