System tags inside attributes

Joseph's Avatar

Joseph

16 Oct, 2010 10:21 PM

I've reviewed the post template regions inside attributes? and system tag as HTML body attribute which are very similar to what I'm trying to do. The latter is probably the direction I should go but after modifying the example Tim provided a few different ways I am unable to reach the desired output.

Here is a snippet of our template:

<div id="contWrapper_3col">
<div class="replace_me">
<system-region name="DEFAULT"/>
</div>
</div>

Here is the XSLT that is applied at the page level (for testing now), not applied on a specific region but at the top level:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/*">
    <xsl:copy>
        <xsl:apply-templates/>
        <xsl:copy-of select="@*"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="node()[@class='replace_me']">
    <xsl:copy>
        <xsl:attribute name="class">blah</xsl:attribute>
        <xsl:copy-of select="@*"/>
        <xsl:copy-of select="./node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="*">
    <xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>

The reason I'd prefer to do this at the template level, is unfortunately because we already have so many format files at this point and to go back through all of them and wrap the right ones in div containers with appropriate class tags would not only take some doing but would be a lot of repeat code. We're trying to wrap the default region in a div that would be replaced based on some dynamic metadata, something like a 'category' field. I'm fairly certain that the line <xsl:template match="node()[@class='replace_me']"> is just not correct for what I'm trying to access. With the current template section and XSLT as they are above, the page element <div class="replace_me"> stays as replace_me instead of changing to 'blah' in this example. Additionally, I would also want to replace the <xsl:attribute name="class">blah</xsl:attribute line with something like <xsl:attribute name="class"><xsl:value-of select="/system-page/category"/></xsl:attribute> where 'category' is a dynamic metadata field that I've already added to metadata set.

Thanks in advance!

  1. 1 Posted by Ross on 17 Oct, 2010 09:23 PM

    Ross's Avatar

    Joseph,
    The key problem is that the two lines…

        <xsl:attribute name="class">blah</xsl:attribute>
        <xsl:copy-of select="@*"/>

    …were cancelling each other out.

    You created an attribute with name() 'class', and then copied all the attributes from the original element. That caused the original class attribute to overwrite the new one. The fix for this is to have it copy all the old attributes except the old class attribute: <xsl:copy-of select="@*[name() != 'class']"/>.

    There are a few other finer points here, though, that could cause you heartache down the road if you apply this globally to your templates:

    • The first <xsl:copy-of select="@*" /> statement will cause an error because it copies the attributes after all the other child nodes. XSLT requires that attribute nodes be copied first.
    • Second, your stylesheet does not match comment() nodes, so it will strip all comments that may be embedded in your Template and Block assets or generated using <xsl:comment> in your other Formats.

    Here's a modified version of your stylesheet that addresses these issues (also attached for download, if you want):

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <!-- BEGIN XSLT pass-through copy templates -->
        <xsl:template match="/">
            <xsl:apply-templates />
        </xsl:template>
    
        <xsl:template match="@*|*" priority="-1">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="comment()">
            <xsl:copy-of select="."/>
        </xsl:template>
    
        <xsl:template match="text()">
            <xsl:value-of select="."/>
        </xsl:template>
        <!-- END XSLT pass-through copy templates -->
    
        <xsl:template match="*[@class='replace_me']">
            <xsl:copy>
                <xsl:attribute name="class">blah</xsl:attribute>
                <xsl:copy-of select="@*[name() != 'class']"/>
                <xsl:copy-of select="./node()"/>
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>
  2. Ross closed this discussion on 17 Oct, 2010 09:23 PM.

  3. Joseph re-opened this discussion on 18 Oct, 2010 06:43 PM

  4. 2 Posted by Joseph on 18 Oct, 2010 06:43 PM

    Joseph's Avatar

    Thanks again Ross! This got us really close to what we need but now I'm noticing a bit of an issue. After applying this XSLT to a page one of our navigation menus, which is rendered in PHP placed inside an XSLT applied to an index block, is being completely erased in the published page.

    When I comment out the replace_me template below the pass-through copy template the results are the same. The navigation PHP spits out markup in the form of <ul><li class="class_name"><a href="/heref/here">Name</a></li>[more li tags]</ul>. There is a

      tag within the navigation XSLT that is coming out in the published page but everything within it is blank.

      When I unlink the rewrite replace me XSLT from the page and publish, the navigation comes out as expected.

      As always, thanks a bunch!

  5. 3 Posted by Joseph on 21 Oct, 2010 04:28 PM

    Joseph's Avatar

    Sorry for the bump, we're really hoping to implement this soon.

  6. 4 Posted by Ross on 25 Oct, 2010 02:40 PM

    Ross's Avatar

    Hm,

    Can you send me your actual source XML? An example of the page without the
    transformation would be fine; you don't need to create anything special.

    Cheers,

    Ross

    On Thu, Oct 21, 2010 at 12:30 PM, Joseph <
    [email blocked]<tender%[email blocked]>
    > wrote:

  7. 5 Posted by Joseph on 25 Oct, 2010 08:38 PM

    Joseph's Avatar

    Hi Ross,

    Thanks again for the reply, I have all the example files but the before and after published page have quite a bit of markup and other text referring directly to our client so I'm not sure if posting it on a public forum is the way to go. Is there a more private way for me to get this to you? I want to make sure you have all the information necessary to get us where we'd like to go.

    Thanks Ross,

    -Joe

  8. 6 Posted by Ross on 02 Mar, 2011 09:11 PM

    Ross's Avatar

    My goodness, Joe! I'm sorry that i let this ball drop and just found this while I was going through old forum posts. Do you still need help with this issue?

    Ross

  9. Tim closed this discussion on 02 Jun, 2011 08:47 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac