Child nodes need to inherent value from parent - all at same level

J

johkar

My problem is that all the nodes that I am concerned about are at the
same level. The parent nodes are marked below; parent nodes have no
cov_code value and their childs do have a cov_code value. Essentially
cov_code without a value serves as a delimiter between groups. In my
XSLT I am outputting XML and need all the SEQ_NBR nodes of the parent
an childs to match so:

Parent1 and children SEQ_NBR 1,1,1

Parent2 and children SEQ_NBR 2,2,2

The ONLY thing I can count on is that the Parent;'s SEQ_NBR will
always be correct and the parent's SEQ_GRP number after the "-" will
always be 0.


<myRoot>
<coverage>
<cov_supp>
<cov_code/><!-- PARENTS HAVE NO VALUE-->
</cov_supp>
<CommlCoverage>
<SEQ_GRP>1-0</SEQ_GRP>
<SEQ_NBR>1</SEQ_NBR><!-- PARENT - THE FIRST PARENT IS 1-->
</CommlCoverage>
</coverage>
<coverage>
<cov_supp>
<cov_code>someval</cov_code><!-- CHILDS HAVE A VALUE VALUE-->
</cov_supp>
<CommlCoverage>
<SEQ_GRP>1-1</SEQ_GRP>
<SEQ_NBR>2</SEQ_NBR><!-- NEED TO OVERRIDE THIS VALUE TO THAT OF ITS
PARENT-->
</CommlCoverage>
</coverage>
<coverage>
<cov_supp>
<cov_code>someval</cov_code>
</cov_supp>
<CommlCoverage>
<SEQ_GRP>1-2</SEQ_GRP>
<SEQ_NBR>1</SEQ_NBR><!-- NEED TO OVERRIDE THIS VALUE TO THAT OF ITS
PARENT-->
</CommlCoverage>
</coverage>
<coverage>
<cov_supp>
<cov_code/><!-- PARENTS HAVE NO VALUE-->
</cov_supp>
<CommlCoverage>
<SEQ_GRP>2-0</SEQ_GRP>
<SEQ_NBR>2</SEQ_NBR><!-- PARENT - SECOND PARENT IS 2 AND SO ON-->
</CommlCoverage>
</coverage>
<coverage>
<cov_supp>
<cov_code>someval</cov_code>
</cov_supp>
<CommlCoverage>
<SEQ_GRP>2-1</SEQ_GRP>
<SEQ_NBR>1</SEQ_NBR><!-- NEED TO OVERRIDE THIS VALUE TO THAT OF ITS
PARENT-->
</CommlCoverage>
</coverage>
<coverage>
<cov_supp>
<cov_code>someval</cov_code>
</cov_supp>
<CommlCoverage>
<SEQ_GRP>2-2</SEQ_GRP>
<SEQ_NBR>2</SEQ_NBR><!-- NEED TO OVERRIDE THIS VALUE TO THAT OF ITS
PARENT-->
</CommlCoverage>
</coverage>
</myRoot>
 
J

Joe Kesselman

I'm not sure what you mean by "inherit" in this context. And "parent"
and "child" are confusing here.

It sounds like what you really want is: For all coverage elements which
have a non-empty cov_supp/cov_code, replace their Comm1Coverage/SEQ_NBR
with the one from the most recent coverage element that _does_ have
empty cov_supp/cov_code.

Or, restructuring that into something easier to write as a stylesheet:

"Copy the document unchanged, EXCEPT: When you see a SEQ_NBR, replace
its contents with those found on the one whose related cov_code is empty."

In other words, start with the identity transformation and add something
like

<xsl:template match="SEQ_NBR">
<!-- Replace with "parent's" SEQ_NBR:
Go up two levels to the containing coverage, look for
the nearest coverage element (which may be the one which
contains this SEQ_NBR) whose cov_code
is empty, take the first one found
(most recent "parent"), and copy its SEQ_NBR
subtree unchanged rather than the one we're starting
from.
-->
<xsl:copy-of
select="/../../
(self::coverage | preceding-sibling::coverage)
[not ../../cov_supp/cov_code/node()]
[1]
/Comm1Coverage/SEQ_NBR"
/>
</xsl:template>

Untested. Could be made more efficient. But it illustrates how to
approach this sort of problem.

Better approach is to beat whoever created this document format over the
head with your favorite W3C Recommendation document until they learn
that the right approach in XML is to make parents _PARENTS_, not
siblings. That would tremendously simplify looking up the parent's
information, yielding better efficiency, more readable documents, more
readable code, lower cost of maintenance...
 
J

johkar

I'm not sure what you mean by "inherit" in this context. And "parent"
and "child" are confusing here.

It sounds like what you really want is: For all coverage elements which
have a non-empty cov_supp/cov_code, replace their Comm1Coverage/SEQ_NBR
with the one from the most recent coverage element that _does_ have
empty cov_supp/cov_code.

Or, restructuring that into something easier to write as a stylesheet:

"Copy the document unchanged, EXCEPT: When you see a SEQ_NBR, replace
its contents with those found on the one whose related cov_code is empty."

In other words, start with the identity transformation and add something
like

<xsl:template match="SEQ_NBR">
        <!-- Replace with "parent's" SEQ_NBR:
                Go up two levels to the containing coverage, look for
                the nearest coverage element (which may be the one which
                contains this SEQ_NBR) whose cov_code
                is empty, take the first one found
                (most recent "parent"), and copy its SEQ_NBR
                subtree unchanged rather than the one we're starting
                from.
        -->
         <xsl:copy-of
             select="/../../
                (self::coverage | preceding-sibling::coverage)
                        [not ../../cov_supp/cov_code/node()]
                        [1]
                /Comm1Coverage/SEQ_NBR"
        />
</xsl:template>

Untested. Could be made more efficient. But it illustrates how to
approach this sort of problem.

Better approach is to beat whoever created this document format over the
head with your favorite W3C Recommendation document until they learn
that the right approach in XML is to make parents _PARENTS_, not
siblings. That would tremendously simplify looking up the parent's
information, yielding better efficiency, more readable documents, more
readable code, lower cost of maintenance...

Thanks for the reply and yes I agree on the format being whacked.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top