Inspect Publication Details with an XSLT Component Template

Ever need in-depth publication information from within your content management explorer (CME aka Tridion's GUI)? Curious about what XSLT can do for you?

I was hoping to make a every-example XSLT training component template (CT) with examples and everything you'd ever want to know about XSLT with Tridion, but I'll have to pace myself and settle for the following. See below on how to make an XSLT CT "inpsector" template. Preview it with any component to get publication details in a recursively-drawn HTML table.

Step 1 Create a new Component Template of type XSLT.

Details
  • Call it something like: "Publication Info (use on any component)"
  • Output format: XML Document or HTML Document (we're just previewing so either work)
  • Priority: any, but I chose "Never Link"
  • Component Presentations based on this Component Template will be:
    Published on a Page



Step 2

Remove any text in the source tab and then Copy and Paste the following, making sure "XSLT" is selected as the template type:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:tcm="http://www.tridion.com/ContentManager/5.0"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  exclude-result-prefixes="xsl tcm xlink">
  <xsl:output
    omit-xml-declaration="no"
    indent="yes"
    method="xml"
    cdata-section-elements="description" />
  <xsl:variable name="Content" select="/tcm:Component/tcm:Data/tcm:Content" />
  <xsl:template match="/">
    <xsl:comment>
      Component ID: <xsl:value-of select="tcm:Component/@ID" />&gt;
    </xsl:comment>

    <h2>Publication XML Breakdown</h2>
    <!-- the document "jump" XSL function needed to follow the publication ID into its nodes,
    this would be similar to getting linked component data -->

    <!-- Tired of long Xpaths? save them to a variable to re-use the variable name with a $ preface -->

    <xsl:variable name="Publication" select="document(tcm:Component/tcm:Context/tcm:Publication/@xlink:href)" />

    <xsl:call-template name="breakdownchildnodes">
      <xsl:with-param name="path" select="$Publication" />
    </xsl:call-template>

    <h3>Or manually get items...</h3>

    <ul>
      <li>
        Publication: 
<xsl:value-of select="tcm:Component/tcm:Context/tcm:Publication/@xlink:title" />
      </li>
      <li>
        Publication URL: 
<xsl:value-of select="$Publication/tcm:Publication/@xlink:href" />
      </li>
      <li>
        Publication Webdav: 
<xsl:value-of select="$Publication/tcm:Publication/tcm:Info/tcm:LocationInfo/tcm:WebDAVURL" />
      </li>
      <li>
        Title: 
<xsl:value-of select="$Publication/tcm:Publication/tcm:Data/tcm:Title" />
      </li>
      <li>
        Path: 
<xsl:value-of select="$Publication/tcm:Publication/tcm:Data/tcm:PublicationPath" />
      </li>
      <li>
        URL: 
<xsl:value-of select="$Publication/tcm:Publication/tcm:Data/tcm:PublicationURL" />
      </li>
      <li>
        Binary Path (Images): 
<xsl:value-of select="$Publication/tcm:Publication/tcm:Data/tcm:MultimediaPath" />
      </li>
      <li>
        Binary URL (Images): 
<xsl:value-of select="$Publication/tcm:Publication/tcm:Data/tcm:MultimediaURL" />
      </li>
      <li>
        Parent Publication(s): 
<xsl:for-each select="$Publication/tcm:Publication/tcm:Data/tcm:ParentPublications/tcm:ParentPublication">
          <xsl:value-of select="./@xlink:title" />
          <xsl:if test="position() != last()">
            <xsl:text>,  </xsl:text>
          </xsl:if>
        </xsl:for-each>
      </li>
    </ul>

    <!-- this is a "cheat sheet" function to pull back EVERYTHING in the source XML
after I have all the nodes I want, I comment this out or remove it -->
    <!-- <xsl:copy-of select="." /> -->

  </xsl:template>

  <xsl:template match="*">
    <xsl:copy-of select="." />
  </xsl:template>
  <xsl:template name="breakdownchildnodes">
    <xsl:param name="path">this path be replaced by calling template</xsl:param>
    <xsl:param name="longname"></xsl:param>

    <xsl:variable name="name" select="local-name()" />
    <!--<xsl:variable name="fullname" select="name()" />-->
    <tr>
      <td>
        <xsl:value-of select="$name" />
        <br />
        <!--<xsl:value-of select="$fullname" />-->
        <xsl:if test="./@*">
          <br />
          <ul>
            <xsl:for-each select="./@*">
              <li>
                <xsl:value-of select="local-name(.)" />="<xsl:value-of select="." />"
              </li>
            </xsl:for-each>
          </ul>
        </xsl:if>
      </td>
      <xsl:if test="./text()">
        <td>
          <xsl:value-of select="." />
        </td>
      </xsl:if>
      <xsl:if test="$path/*">
        <td>
          <xsl:for-each select="$path/*">
            <table border="1">
              <xsl:call-template name="breakdownchildnodes">
                <xsl:with-param name="path" select="." />
              </xsl:call-template>
            </table>
          </xsl:for-each>
        </td>
      </xsl:if>
    </tr>
  </xsl:template>
</xsl:stylesheet>

Step 3 Preview

Right-click on the template in a publication that has components and preview any component to see the publication information. Optionally set a specific schema as a linked schema (Linked Schema tab in the XSLT CT) to allow the preview to start from the components based on that schema.

Operational notes
To use something like this in a real scenario...
  • be sure to put the template where end users don't have access to it, possibly in a folder that only admins have access to
  • don't set a linked schema along with setting the template to publish dynamically (otherwise it will get published to the storage/broker database)
  • carefully choose which templates belong on your DEV content management system (CMS) versus your other environments -- simple or well tested templates can go anywhere, but "starter" templates probably only belong on DEV

Tridion has a lot of hooks including the new GUI extensions for the 2011 platform, but a "quick and dirty" yet still somewhat elegant solution to getting data, reports, or other information out of the CMS is through templates such as this one. Consider developing your own similar XSL (or other templating language) "utility templates" as starter examples or to pull data in the format you need within the CME.

I'd love to see other shortcuts and utility templates you've made to inspect content, get categories and keywords, or otherwise create simple reports using preview or possibly publishing. Happy template building! 
(20-Dec-2011) Update: made code prettier.

No comments:

Post a Comment

Feel free to share your thoughts below.

Some HTML allowed including links such as: <a href="link">link text</a>.