Search code examples
tridiontridion-2011

how to get the list of keywords if we have category tcm id in XSLT TBB(XSLT Mediator)


I am using SDL tridion 2011 SP1.

I want to get the list of keywords under given category using XSLT Mediator.

Have any one come across this situation, If yes please share your views.

But When I actually looked at the Category Item XML it does not have any info related to its keywords.


Solution

  • You will need to create a C# TBB to insert the category keywords into the package, and then access this as a parameter in your XSLT.

    You can use a piece of c# like the following:

    class GetCategoryKeywords : TemplateBase
        {
            public override void Transform(Engine engine, Package package)
            {
                Initialize(engine, package);
                String webDavPathCategory = package.GetValue("CategotryWebDavPath");
                Category cat = (Category)engine.GetObject(webDavPathCategory);
                XmlDocument keywordsXml = new XmlDocument();
                keywordsXml.LoadXml(cat.GetListKeywords().OuterXml);
                Item output = package.CreateXmlDocumentItem(ContentType.Xml, keywordsXml);
                package.PushItem("CategoryKeywords", output);
            }
        }
    

    This will place an XML document in the package called CategoryKeywords containing the keywords. Then when you invoke the XSLT mediator, set the "Include Package Paramters" value to true, and add a parameter to the top of your XSLT as follows:

    <xsl:param name="CategoryKeywords"/>
    

    You can then loop through the new parameter as a variable performing any XPath queries on it that you desire. The following samples may help:

    <xsl:variable name="URI" select="$CategoryKeywords//tcm:ListUsedItems/tcm:Item[@Title=$VALUE]/@ID" />
    
    <xsl:for-each select="$CategoryKeywords//tcm:ListItems/tcm:Item">
        Do something
    <xsl:for-each>