In this code example, I have 2 tasks: install B1 node after B node and before node C, D and E, and the second call is to add the second KEY node to / ROOT / E / OTHER / DEAL / KEYS.
This XML sample:
<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>
after conversion:
<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>
<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>
<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>
steel:
<?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>
why is the second template definition completely ignored?
. B1 node B node node C, D E B1 node , : C, D E.
= "ROOT/E/OTHER/DEAL/KEYS", : KEY node /ROOT/E/OTHER/DEAL/KEYS, ,
, ROOT node, match = "ROOT/E/OTHER/DEAL/KEYS" , , , . xsl: template match = "ROOT/E/OTHER/DEAL/KEYS... xsl: for-each select =... xsl: call-template name=" add-KEYS ", .
, apply-templates, . XSLT , , , .
"apply-templates" XML, " ", "" , , , 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>