1

I have a general question regarding <xsl:template match=""> and <xsl:apply-templates/>.

I have the case where I match the element E1EDP01. But no all E1EDP01 elements should be matched, only a certain range.

But now what is the correct way of matching only a certain range of elements?

Is it correct to do it this way:

<xsl:apply-templates select="E1EDP01"/>

and the template that restricts the range of E1EDP01:

<xsl:template match="E1EDP01[not(PSTYV='ZDAE')][E1EDP02[QUALF='001']/ZEILE]">
...
</xsl:template>

Or do I have to change the apply-templates to:

<xsl:apply-templates select="E1EDP01[not(PSTYV='ZDAE')][E1EDP02[QUALF='001']/ZEILE]"/>
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Peter
  • 1,786
  • 4
  • 21
  • 40

2 Answers2

4

Both work, but they express slightly different intentions. The specific matching template says "this is how you should always process this kind of E1EDP01 element", and the <xsl:apply-templates select="E1EDP01"/> says "process all my E1EDP01 elements now". Whereas having a generic template and a specific apply says "I want to process these particular E1EDP01 elements now (but I might want to process others later)" and "this is how you process any E1EDP01".

Which approach is better really depends on whether the [not(PSTYV='ZDAE')][E1EDP02[QUALF='001']/ZEILE] is something inherent to the way the elements should be handled, or something specific to what you want to do at one particular place in the stylesheet. For example, if I had some XML describing financial transactions and I had a rule that negative amounts always had to be displayed in a red box, then I might define

<xsl:template match="amount[. &lt; 0]">
  <redbox><xsl:value-of select="."/></redbox>
</xsl:template>
<xsl:template match="amount"><xsl:value-of select="."/></xsl:template>

If instead I wanted to include a summary redbox with all the negative amounts, but display the amounts normally elsewhere then I would probably choose to use a single template for amount but then filter at the apply-templates point

<redbox>
  <xsl:apply-templates select="amount[. &lt; 0]" />
</redbox>

You have to choose the approach that makes most sense for your task.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • Hello Ian, thank you for your detailed answer. Yes, they both work. I was wondering if one might also be to prefer due to performance issues? I do not really understand what you mean in your second paragraph though. I do something specific with all E1EDP02 elements that match the criteria, all the others are not considered. Could you explain the 2nd paragraph again please? Thank you, Peter PS: for example the comment from Dimitre under here: http://stackoverflow.com/questions/12404116/nested-loops-in-xslt-for-dynamically-building-xml/12405591#12405591 to not use // in the match-pattern. – Peter Feb 19 '13 at 13:51
  • Hello Ian, thank you for your edit. I think I will go with filtering at the because I specifically only want a certain range to be processed (in your example into the ). I think I got it. Best regards, Peter +1 – Peter Feb 20 '13 at 20:47
1

If both work for what you're trying to do, just pick the one that makes the most sense for your XSLT.

djc
  • 11,603
  • 5
  • 41
  • 54