简体   繁体   English

如何使用XSLT介体在tridion SDL Tridion 2011 SP1中处理XSLT中的链接组件

[英]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. 我正在为一个链接到另一个组件的组件创建XSLT TBB。

Consider my Component name is "A" which has link to another component "B". 考虑我的组件名称是“A”,它具有到另一个组件“B”的链接。

Component A source looks like this: 组件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"></Name>
</Content>

Component B source is: 组件B来源是:

<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? 任何人都可以帮我编写一个XSLT TBB来输出这个链接组件的值吗?

Thank you. 谢谢。

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? 您是否想要在内容管理器端访问TBB中的此链接组件及其字段,或者您是否要在HTML中输出将链接到您网站上其他组件的锚标记?

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 组件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 组件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: 然后,您可以转换组件A并访问链接的组件B,如下所示:

<?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. 请注意,我在XPath中使用了“//”来使代码更容易阅读,但从性能角度来看,这并不理想。

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. 如果由于某种原因你不知道链接组件将基于什么Schema(以及命名空间),你也可以使用$LINKED_COMPONENT//*[local-name()='Text']表示法,但这又会引入性能打击。

To output an image that your Component links to, have a look at this: http://yoavniran.wordpress.com/2009/07/11/implementing-the-xslt-mediator-part-1 要输出组件链接到的图像,请查看以下内容:http: //yoavniran.wordpress.com/2009/07/11/implementing-the-xslt-mediator-part-1

<xsl:element name="img">
    <xsl:attribute name="src">
        <xsl:value-of select="simple:image/@xlink:href"/>
    </xsl:attribute>
</xsl:element>

Edit: To output additional fields of a linked Component, see this section: http://yoavniran.wordpress.com/2009/07/11/implementing-the-xslt-mediator-part-1/#complink 编辑:要输出链接组件的其他字段,请参阅此部分:http: //yoavniran.wordpress.com/2009/07/11/implementing-the-xslt-mediator-part-1/#complink

An example from there: 一个例子:

<xsl:attribute name="alt">
    <xsl:value-of select="document(simple:image/@xlink:href)/tcm:Component/tcm:Data/tcm:Metadata/image:Metadata/image:altText"/>
</xsl:attribute>

So this loads the Multimedia Component and then extracts a value from a Metadata field. 因此,这会加载多媒体组件,然后从元数据字段中提取值。

Here is XSLT code to extract some fields from multi-value component links. 以下是从多值组件链接中提取某些字段的XSLT代码。 Keep in mind that component links belongs to the same schema. 请记住,组件链接属于同一模式。

<!-- language: xml -->
<xsl:for-each select="base:componentLink">
<xsl:element name="div">      
  <xsl:variable name ="LinkedComponent" select="document(./@xlink:href)"></xsl:variable>
  <xsl:value-of select="$LinkedComponent/tcm:Component/tcm:Data/tcm:Content/linked:Teaser/linked:LinkText"/>      
</xsl:element>
</xsl:for-each>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM