I'm writing an XSLT that, among other things, locates certain elements that have a "name" attribute with a value that starts with a dash ('-'). When such attribute is found, the xslt creates an xsl:attribute, which name is all the text that comes after the "name" attribute's dash.
So, for instance, suppose I have the following XML segment:
<someelement>
<json:string name="-style">display: block; white-space: pre; border: 2px
solid #c77; padding: 0 1em 0 1em; margin: 1em;
background-color: #fdd; color: black</json:string>
[... some extra elements here ...]
</someelement>
And I want it to become
<someelement style="display: block; white-space: pre; border: 2px solid #c77;
padding: 0 1em 0 1em; margin: 1em; background-color: #fdd;
color: black">
[... some extra elements here ...]
</someelement>
Currenty, I'm trying several variations on the following XSLT:
<!-- Match string|number|boolean|null elements with a "name" attribute -->
<xsl:template match="json:string[@name] | json:number[@name] | json:boolean[@name] | json:null[@name]">
<xsl:choose>
<xsl:when test="starts-with(@name,'-')">
<xsl:attribute name="{substring(./[@name],2,string-length(./@name) - 1)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{@name}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
And to be specific, this is the line that puzzles me:
<xsl:attribute name="{substring(./[@name],2,string-length(./@name) - 1)}">
To be even more specific, the part in it which doesn't work is the string-length part. If I replace the entire string-length part with a number, say 2, it works just fine.
And yes, I know that substring-after would suite me better. I did try the following, but it doesn't work either:
<xsl:attribute name="{substring-after(./[@name],'-')}">
I'm sure this is some kind of syntactic error.
P.S. - I'm using XMLSPY for my tests.
Your help is very much appreciated.