Reputation: 1756
I have an XML file with multiple elements. I'm using XSLT to transform each to a unique XML file. Each has an id attribute that I want to use in the file name and I'm trying to clean up the attribute values. The ids all start with capi_ and some end with _output, so the format is capi_xxx_yyy_output or capi_xxx_yyy.
I can extract the string after capi_:
<xsl:for-each select="//section">
<xsl:variable name="cleanId" select="substring-after(@id, '_')" />
From this new string, how do I grab the string before _output? I tried this but it didn't work:
<xsl:for-each select="//section">
<xsl:variable name="cleanId" select="substring-before(substring-after(@id, '_'), '_output')" />
Thanks in advance.
Upvotes: 0
Views: 1160
Reputation: 9012
Your syntax looks good, you may simply need to implement an <xsl:choose>
.
<xsl:variable name="cleanId">
<xsl:for-each select="//section">
<xsl:choose>
<xsl:when test="contains(@id,'_output')">
<xsl:value-of select="substring-before(substring-after(@id, '_'), '_output')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-after(@id, '_')" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
Upvotes: 0
Reputation: 117140
I tried this but it didn't work:
<xsl:for-each select="//section"> <xsl:variable name="cleanId" select="substring-before(substring-after(@id, '_'), '_output')" />
It works when the id ends with "_output"; it stops working when it doesn't. So just make sure it always does:
<xsl:variable name="cleanId" select="substring-before(substring-after(concat(@id, '_output'), '_'), '_output')" />
Upvotes: 1