tags:

views:

23

answers:

1

Hello, everyone

I'm working on a XSLT file and I've hit a snag. I have a case where one tag can have two different subtags. Example:

<TAG>
 <PERSON1/>
<TAG>

and

<TAG>
 <PERSON2/>
</TAG>

Is there a way to write an XLS:IF statement on the tag to see if it's named "PERSON1" or "PERSON2", since PERSON1 and PERSON2 need to be handled in entirely different manners?

Thanks.

+2  A: 

I don't think you need an if, you could use xsl:apply-templates, so something like this:

<xsl:template match="TAG">
    <!-- Code ... -->
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="PERSON1">
    <!-- Something goes here -->
</xsl:template>

<xsl:template match="PERSON2">
    <!-- Something else goes here -->
</xsl:template>
Buddy
@Buddy: It's good that you recommend the use of "pattern matching". But have an infinite recursion with `<xsl:apply-templates select="."/>`. Should be `<xsl:apply-templates/>`
Alejandro
@Buddy, @Alejando. And there was `name` where there should be `match`. I've taken the liberty of editing. If the querant wanted the "TAG" element itself handled differently in each case they could use `<xsl:template match="TAG[PERSON1]">`... and equiv. `<xsl:if test="PERSON1">` is still possible when it is handier, and `<xsl:choose><xsl:when test="PERSON1">`...
Jon Hanna
@Jon Hanna: +1 for editing.
Alejandro