Cannot apply-templates to child nodes when the context item
Oxygen general issues.
-
- Posts: 3
- Joined: Mon Mar 14, 2011 8:51 pm
Cannot apply-templates to child nodes when the context item
Hello,
I'm trying to understand regexes and variables.
I have an xml file:
and an xslt including:
I want the output to be:
ie the class attribute is included when in the form wwdddddd but empty otherwise.
My xslt is producing:
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
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>
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>
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>
My xslt is producing:
Code: Select all
<addresses>
<address>
<addrLine2 class="sh12345678"/>
<addrLine2 class=""/>
<addrLine2 class=""/>
<addrLine2 class=""/>
</address>
</addresses>
<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
-
- Posts: 2879
- Joined: Tue May 17, 2005 4:01 pm
Re: Cannot apply-templates to child nodes when the context item
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:
Regards,
Adrian
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>
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
-
- Posts: 3
- Joined: Mon Mar 14, 2011 8:51 pm
Re: Cannot apply-templates to child nodes when the context item
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
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
-
- Posts: 2879
- Joined: Tue May 17, 2005 4:01 pm
Re: Cannot apply-templates to child nodes when the context item
Hi,
classValue is not a variable, it's a parameter of the template:
Regards,
Adrian
classValue is not a variable, it's a parameter of the template:
Code: Select all
<xsl:template name="matchClassValue">
<xsl:param name="classValue"/>
...
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ Artificial Intelligence (AI Positron Assistant add-on)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service