このコード例では、B1ノードをBノードの後に、ノードC、DおよびEの前に2つの課題があります。 2番目のKEYノードを/ ROOT/E/OTHER/DEAL/KEYS構造体に追加します。複数のテンプレート定義を適用してそれぞれが同じXML構造を変更する場合
この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>
になる:なぜ2番目のテンプレートの定義が完全に
<?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 。
/ROOT/E/OTHER/DEAL/KEYS構造体に2番目のKEYノードを追加するために、2番目のチャレンジパートを満たす2番目のテンプレートmatch = "ROOT/E/OTHER/DEAL/KEYS"完全に。 この事実に加えて、最初のテンプレートマッチをROOTノードにコメントすると、2番目のテンプレートmatch = "ROOT/E/OTHER/DEAL/KEYS"が正しく動作しますが、実際には追加キーが追加されますが、最初のテンプレートの一致が常に2番目のテンプレートの一致を上書きする理由を知りません。私はxsl:template match = "ROOT/E/OTHER/DEAL/KEYS ...とxsl:for-each select = ...とxsl:call-template name =" add-KEYS "を試してみましたが、何も役に立たなかった
実際にはapply-templatesというノードテンプレートは、より高い優先順位を持つ上位構造のテンプレートと一致します。XSLTファイル内のテンプレートの場所を変更しても、影響はありません。 一致するテンプレートごとに "apply-templates"がXML構造に変更を加えると暗黙のうちに "for-each"が作成されますが、 "advice" 2番目のテンプレートマッチが最初のテンプレートから触れられたことのない別のXPathの場所を探しているので、構造体が変更された2番目のテンプレートと、なぜそれを行うべきですか?私の場合はg?...そしてそれを行う方法のベストプラクティスは何ですか?
期待される結果:
<?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>
ありがとうございました! –
@ user1619586、ようこそ。 –