Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle linked components in XSLT in tridion SDL Tridion 2011 SP1 using XSLT mediator

I am working on creating XSLT TBB for a component that has link to another component.

Consider my Component name is "A" which has link to another component "B".

Component A source looks like this:

<Content xmlns="Some UUID">
    <Name xlink:type="simple" xlink:href="tcm:184-1897" 
          xmlns:xlink="http://www.w3.org/1999/xlink" xlink:title="B"></Name>
</Content>

Component B source is:

<Content xmlns="Some UUID">
    <first>first filed</first>
    <second>second field</second>
</Content>

Can any one help me how to write an XSLT TBB that outputs values from this linked Component?

Thank you.

like image 477
Patan Avatar asked Dec 17 '22 01:12

Patan


2 Answers

In order to access fields from the linked component you will need to load it using the document function, keep i nmind that the linked component may be based on a different Schema, and as such have a different name space like this:

Component A

<Content xmlns="Some UUID">
    <Name xlink:type="simple" 
        xlink:href="tcm:184-1897" 
        xmlns:xlink="http://www.w3.org/1999/xlink" 
        xlink:title="B"/>
</Content>

Component B

<Content xmlns="Some Other UUID">
    <Text>Some Value</Text>
</Content>

You can then transform the Component A and access the linked Component B as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:main="Some UUID" 
    xmlns:link="Some Other UUID" 
    xmlns:xlink="http://www.w3.org/1999/xlink" >
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <xsl:variable name="LINKED_COMPONENT" select="document(//main:Name/@xlink:href)"/>
        <xsl:value-of select="$LINKED_COMPONENT//link:Text"/>
    </xsl:template>
</xsl:stylesheet>

Note that I have used "//" in my XPath to make the code easier to read, but this is not ideal from a performance stand point.

If for some reason you will not know what Schema (and therefore namespace) the linked Component will be based on, you can also use the $LINKED_COMPONENT//*[local-name()='Text'] notation, but this again will introduce a performance hit.

like image 103
Chris Summers Avatar answered Jan 01 '23 09:01

Chris Summers


Please explain what you mean by "handle this component linking".

Do you mean that you want to access this linked component and its fields within your TBB on the content manager side, or do you mean that you want to output an anchor tag in your HTML that will link to the other component on your website?

like image 45
David Forster Avatar answered Jan 01 '23 11:01

David Forster