Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to differentiate two templates in XSL?

Tags:

xml

xslt

This is what I'm trying to do in XSL:

<xsl:apply-templates select="document('a.xml')//row"/>
<xsl:apply-templates select="document('b.xml')//row"/>

<xsl:template match="row">
  <!-- for document a.xml -->
</xsl:template>

<xsl:template match="row">
  <!-- for document b.xml -->
</xsl:template>

Doesn't work as is now, for obvious reasons. How can I differentiate these two templates? Document a.xml and b.xml are absolutely identical in terms of XML structure.

like image 409
yegor256 Avatar asked Nov 19 '25 08:11

yegor256


2 Answers

Use the mode attribute.

<xsl:apply-templates select="document('a.xml')//row" mode="a"/>
<xsl:apply-templates select="document('b.xml')//row" mode="b"/>

<xsl:template match="row" mode="a">
  <!-- for document a.xml -->
</xsl:template>

<xsl:template match="row" mode="b">
  <!-- for document b.xml -->
</xsl:template>
like image 80
Oded Avatar answered Nov 21 '25 23:11

Oded


You can use the mode attribute as suggested, though this does mean that the decision is made partly at the xsl:apply-templates level and partly by the template rule itself. If you want the control to be purely in the template rule, you could use the match patterns

row[(/) is document('a.xml')]
row[(/) is document('b.xml')]

(If you're still using XSLT 1.0, replace "A is B" by "generate-id(A) = generate-id(B)")

like image 24
Michael Kay Avatar answered Nov 21 '25 22:11

Michael Kay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!