Cannot apply-templates to child nodes when the context item

Oxygen general issues.
pollecu
Posts: 3
Joined: Mon Mar 14, 2011 8:51 pm

Cannot apply-templates to child nodes when the context item

Post by pollecu »

Hello,

I'm trying to understand regexes and variables.

I have an xml file:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<addresses>
<address>
<addrLine2 class="sh12345678">some text 1</addrLine2>
<addrLine2 class="history">some text 2</addrLine2>
<addrLine2 class="sh">some text 3</addrLine2>
<addrLine2 class="12345678">some text 4</addrLine2>
</address>
</addresses>
and an xslt including:

Code: Select all

    <xsl:template match="addrLine2">
<xsl:variable name="classValue" select="@class"/>

<xsl:analyze-string select="$classValue"
regex="[a-zA-Z]{{2}}[0-9]+">

<xsl:matching-substring>
<addrLine2 class="{$classValue}"></addrLine2>
</xsl:matching-substring>

<xsl:non-matching-substring>
<addrLine2 class=""></addrLine2>
</xsl:non-matching-substring>

</xsl:analyze-string>

</xsl:template>
I want the output to be:

Code: Select all

<addresses>
<address>
<addrLine2 class="sh12345678">some text 1</addrLine2>
<addrLine2 class="">some text 2</addrLine2>
<addrLine2 class="">some text 3</addrLine2>
<addrLine2 class="">Lsome text 4</addrLine2>
</address>
</addresses>
ie the class attribute is included when in the form wwdddddd but empty otherwise.

My xslt is producing:

Code: Select all

<addresses>
<address>
<addrLine2 class="sh12345678"/>
<addrLine2 class=""/>
<addrLine2 class=""/>
<addrLine2 class=""/>
</address>
</addresses>
But I can't get the addrLine2 "some text 1" etc to output. If I try
<addrLine2 class="{$classValue}"><xsl:apply-templates/></addrLine2>
I just get "Cannot apply-templates to child nodes when the context item is an atomic value".

Any help with this regex or variable (or whatever else it should be!) would be appreciated!

Thanks,

N
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Cannot apply-templates to child nodes when the context item

Post by adrian »

Hello,

The context inside the xsl:analyze-string is represented by its select, so in your case it's a string(the value of the class attribute).

Here's a quick solution. Keep the string match separate(use a named template to do it) when constructing the result document:

Code: Select all

    <xsl:template match="addrLine2">
<xsl:element name="addrLine2">
<xsl:attribute name="class">
<xsl:call-template name="matchClassValue">
<xsl:with-param name="classValue" select="@class"/>
</xsl:call-template>
</xsl:attribute>
<xsl:value-of select="text()"/>
</xsl:element>
</xsl:template>

<xsl:template name="matchClassValue">
<xsl:param name="classValue"/>
<xsl:analyze-string select="$classValue"
regex="[a-zA-Z]{{2}}[0-9]+">

<xsl:matching-substring>
<xsl:value-of select="$classValue"/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>
Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
pollecu
Posts: 3
Joined: Mon Mar 14, 2011 8:51 pm

Re: Cannot apply-templates to child nodes when the context item

Post by pollecu »

Dear Adrian,

That's great - that's produced exactly what I wanted.

Thanks very much,

Natalie
pollecu
Posts: 3
Joined: Mon Mar 14, 2011 8:51 pm

Re: Cannot apply-templates to child nodes when the context item

Post by pollecu »

Hi,

One thing from this I don't understand: you're using the variable $classValue in the named template but I don't see where this variable is declared. I expected to see <xsl:variable name="classValue" etc.

How is it possible to use the variable without first declaring it?

Thanks,

Natalie
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Cannot apply-templates to child nodes when the context item

Post by adrian »

Hi,

classValue is not a variable, it's a parameter of the template:

Code: Select all

<xsl:template name="matchClassValue">
<xsl:param name="classValue"/>
...
Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Post Reply