Counting level of grandparent nodes
Here should go questions about transforming XML with XSLT and FOP.
-
- Posts: 67
- Joined: Thu Jan 24, 2008 4:52 pm
Counting level of grandparent nodes
I have an element structure like the one below.
When processing "F" elements, I'd like to count which "C" element I'm in when processing each "F".
I think this would be like counting previous "C" siblings when processing "F", but I can't figure out the XPath pattern to give me such a count, or something similar, when in the XSL processing for each "F" element.
I can live with 0-based or 1-based counting, as long as values are sequential.
Thanks for suggestions.
-- Mike
When processing "F" elements, I'd like to count which "C" element I'm in when processing each "F".
I think this would be like counting previous "C" siblings when processing "F", but I can't figure out the XPath pattern to give me such a count, or something similar, when in the XSL processing for each "F" element.
I can live with 0-based or 1-based counting, as long as values are sequential.
Thanks for suggestions.
-- Mike
Code: Select all
<C>
<D>
<E Attr1="..." />
<F Attr2="..." /> <!--"C" level would be 0 (or 1) -->
</D>
</C>
<C>
<D>
<E Attr1="..." />
<F Attr2="..." /> <!--"C" level would be 1 (or 2) -->
</D>
</C>
<C>
<D>
<E Attr1="..." />
<F Attr2="..." /> <!--"C" level would be 2 (or 3) -->
</D>
</C>
-
- Site Admin
- Posts: 2095
- Joined: Thu Jan 09, 2003 2:58 pm
Re: Counting level of grandparent nodes
Note that the suggestion was to use preceding::C and not precedinf-sibling::C.
On a document like
the following stylesheet
will produce
Regards,
George
On a document like
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<test>
<C>
<D>
<E Attr1="..."></E>
<F Attr2="..."></F>
<!--"C" level would be 0 (or 1) -->
</D>
</C>
<C>
<D>
<E Attr1="..."></E>
<F Attr2="..."></F>
<!--"C" level would be 1 (or 2) -->
</D>
</C>
<C>
<D>
<E Attr1="..."></E>
<F Attr2="..."></F>
<!--"C" level would be 2 (or 3) -->
</D>
</C>
</test>
Code: Select all
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/">
<result><xsl:apply-templates/></result>
</xsl:template>
<xsl:template match="F">
<inF>Processing F having count of preceding Cs: <xsl:value-of select="count(preceding::C)"/></inF>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<result>
<inF>Processing F having count of preceding Cs: 0</inF>
<inF>Processing F having count of preceding Cs: 1</inF>
<inF>Processing F having count of preceding Cs: 2</inF>
</result>
George
George Cristian Bina
-
- Posts: 67
- Joined: Thu Jan 24, 2008 4:52 pm
Re: Counting level of grandparent nodes
Dear jkmyoung and George,
In trying to simplify my example, I simplified too much. Please note that I did try preceding as well as preceding-sibling in my first try.
My error: Namespaces!
I needed count(preceding::mynamespace:C)
Sorry about the confusion, and many thanks for your patience as you steered me to the solution.
Thanks again, and apologies,
Mike
In trying to simplify my example, I simplified too much. Please note that I did try preceding as well as preceding-sibling in my first try.
My error: Namespaces!
I needed count(preceding::mynamespace:C)
Sorry about the confusion, and many thanks for your patience as you steered me to the solution.
Thanks again, and apologies,
Mike
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ 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