2
votes

Given an XML like this:

<a id="1">
  <b>
    <code>42</code>
  </b>
</a>

And the target being:

<request>
  <aId>1</aId>
  <bCode>42</bCode>
</request>

I can reach that using this XSLT:

<template match="/">
  <element name="request">
    <apply-templates/>
  </element>
</template>

<template match="a">
  <element name="aId"><value-of select="@id"/></element>
  <apply-templates/>
</template>

<template match="b/code">
  <element name="bCode"><value-of select="."/></element>
</template>

However, this also works if I swap <apply-templates/> with <next-match/>. Any advice on which to use when, when they both seem to work fine? Does next-match have additional effects which can mess things up if I add more stuff later for example?

1
xsl:next-match is useful when you need to perform "staged processing" -- and this is clearly not your current case.Dimitre Novatchev

1 Answers

1
votes

Well in that case your use of next-match relies on the built-in element template doing apply-templates (see http://www.w3.org/TR/xslt20/#built-in-rule). And once you added a template for elements e.g.

<xsl:template match="*">
  <xsl:copy>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

the next-match would no longer use the built-in template which does nothing but apply-templates but would choose above template which adds result nodes.

So for your code I would continue to use apply-templates.