Reputation: 71
This is the code I have:
<xsl:template match="*">
<Segment ID="{name()}">
<xsl:value-of select="concat(., ' ')"/>
</Segment>
</xsl:template>
<xsl:template match="*" mode="break">
<Segment ID="{name()}">
<xsl:value-of select="."/>
</Segment>
<Segment>#$NL</Segment>
</xsl:template>
Is there a way to call the first template inside the second template so that I don't have to repeat the code? Something like this:
<xsl:template match="*" mode="break">
<xsl:apply-templates select="*"/>
<Segment>#$NL</Segment>
</xsl:template>
I use * here and it doesn't work. I tried @* and / but nothing works. The goal is for me to call it like this:
<xsl:apply-templates select="FirstName"/>
or
<xsl:apply-templates select="Name" mode="break"/>
Upvotes: 0
Views: 155
Reputation: 1509
If you use xsl:call-template
rather than xsl:apply-templates
, you can set up a named template and then call it whenever you need it.
If I understand your question correctly, the repeated code is a Segment element with specified ID attribute and inner text string. So you can give this template those two parameters, and specify them when you call it. Like this:
<xsl:template name="segment">
<xsl:param name="id"/>
<xsl:param name="text"/>
<Segment ID="{$id}">
<xsl:value-of select="$text"/>
</Segment>
</xsl:template>
...
<xsl:call-template name="segment">
<xsl:with-param name="id" select="name(.)"/>
<xsl:with-param name="text" select="."/>
</xsl:call-template>
Upvotes: 1
Reputation: 101652
You can simply replace the two templates you showed us with these two:
<xsl:template match="*" name="Segment">
<Segment ID="{name()}">
<xsl:value-of select="concat(., ' ')"/>
</Segment>
</xsl:template>
<xsl:template match="*" mode="break">
<xsl:call-template name="Segment" />
<Segment>#$NL</Segment>
</xsl:template>
I can see that in your original XSLT, the first template was concatenating a space onto the value, and the second wasn't. With the above approach, the space will be appended in both cases. Is that satisfactory for your requirements?
Upvotes: 1