insert xml into a node of parent xslt

Hi all,

Please consider the following two xsl stylesheets:

parent.xslt

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="html" omit-xml-declaration="yes" standalone="yes" indent="yes" />
      <xsl:template match="/">
            <html>
                  <head>
                        <title>Some page</title>
                        <!-- How do I insert content here from child.xslt? -->                        
                  </head>
                  <body>
                        <xsl:call-template name="HtmlBodyContent" />
                  </body>
            </html>
    </xsl:template>
</xsl:stylesheet>

and child.xslt

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="html" omit-xml-declaration="yes" standalone="yes" indent="yes" />
      <xsl:include href="parent.xslt" />

      <!--
      But how can I insert content in the html head in the parent.xslt from here?
      or from somewhere else in this child.xslt
      -->

      <xsl:template name="HtmlBodyContent">
            Body content goes here
      </xsl:template>
</xsl:stylesheet>

When reasoning top down ( from parent to child ) there is no problem. A child.xslt must implement a template named HtmlBodyContent and all is well. Also going deeper than one level is no problem.

However when reversing the direction ( from child to parent ) I don't see how I can insert content into a node that is contained in a parent or ansestor stylesheet.

Please check my stylesheets and look for the comments in it, it should help explain what I need to do.

Working from child to ansestor is important because some files have multiple levels and each level needs to be able to add to the html head ( to add script blocks )

Any ideas,

Kind regards
LVL 2
DaFouAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zc2Commented:
Please look at the following sample. May be that will work for you.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="html" omit-xml-declaration="yes" standalone="yes" indent="yes" />
      <xsl:template name="HTMLwrap">
        <xsl:param name="head"/>
        <xsl:param name="body"/>
            <html>
                  <head>
                        <title>Some page</title>
                        <xsl:copy-of select="$head" />
                  </head>
                  <body>
                        <xsl:copy-of select="$body" />
                  </body>
            </html>
    </xsl:template>
</xsl:stylesheet>

Open in new window


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="html" omit-xml-declaration="yes" standalone="yes" indent="yes" />
      <xsl:include href="parent.xslt" />

      <xsl:template match="/">   <!-- This is the main entry -->
       <xsl:call-template name="HTMLwrap">
        <xsl:with-param name="head">
            Head content goes here
        </xsl:with-param>
        <xsl:with-param name="body">
            Body content goes here
        </xsl:with-param>
        </xsl:call-template>
      </xsl:template>
</xsl:stylesheet>

Open in new window

DaFouAuthor Commented:
thanks zc2 for your suggestion.

Could you please elaborate on this idea a bit more with an example of 3 levels?

parent.xslt, child.xslt and grandchild.xslt.

both child.xslt and grandchild.xslt should be able to add content to the html head.

kind regards
Gertone (Geert Bormans)Information ArchitectCommented:
Well, For just a parent-child relation I would have taken the approach z2c has taken.
You seem to want ulimate flexibility however.
Then, I can recommend an aproach with placeholders in a seperate file

haven XML file "head.xml" like this
<head>
    <?head-info-grand-child?>
    <?head-info-child?>
    <?head-info-parent?>
    <?head-info-grand-parent?>
    <?head-info-sisters-of-mercy?>
    <?head-info-brother-in-arms?>
    <?head-info-drunken-cousin?>
</head>

And this is how the three XSLT files would look like (note I often prefer import over include because it pushes down priority levels)
grand-child.xslt

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:import href="child.xslt" />
    <xsl:output method="html" omit-xml-declaration="yes" standalone="yes" indent="yes" />
    
    <xsl:template match="processing-instruction()[name() = 'head-info-grand-child']" mode="head">
        <head-stuff master="grand-child"/>
    </xsl:template>
    
    <xsl:template name="HtmlBodyContent">
        Body content goes here
    </xsl:template>
</xsl:stylesheet>

child.xslt

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:import href="parent.xslt" />
    <xsl:output method="html" omit-xml-declaration="yes" standalone="yes" indent="yes" />
    
    <xsl:template match="processing-instruction()[name() = 'head-info-child']" mode="head">
        <head-stuff master="child"/>
    </xsl:template>
    
</xsl:stylesheet>

parent.xslt

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" omit-xml-declaration="yes" standalone="yes" indent="yes" />
    <xsl:template match="/">
        <html>
            <head>
                <title>Some page</title>
                <xsl:apply-templates select="document('head.xml')/head/processing-instruction()" mode="head"/>                        
            </head>
            <body>
                <xsl:call-template name="HtmlBodyContent" />
            </body>
        </html>
    </xsl:template>
    
    <!-- add parent Head info -->
    <xsl:template match="processing-instruction()[name() = 'head-info-parent']" mode="head">
        <head-stuff master="parent"/>
    </xsl:template>

    <!-- remove redundant PI from head-->
    <xsl:template match="processing-instruction()" mode="head"/>
</xsl:stylesheet>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.