如何从 XSLT TBB 获取关键字的“键"? [英] How can I get the 'key' of a keyword from an XSLT TBB?

查看:26
本文介绍了如何从 XSLT TBB 获取关键字的“键"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 XSLT TBB(在 Tridion 2011 SP1 上使用 XSLT Mediator)从关键字中检索键值.

I am working on the XSLT TBB (using the XSLT Mediator on Tridion 2011 SP1) to retrieve the key Value from the Keyword.

我的关键字看起来像这样.

My Keyword looks like this.

Value: Some Value   
Key: Its ID is 123

这是一个普通的关键字.

It's a normal Keyword.

我创建了一个带有字段的架构.将从列表和类别中选择值.

I have created a schema with a field. The values will be selected from List and from Category.

组件 Source 如下所示:这是直接取自 Tridion UI 组件的组件源.

The component Source looks like this: This is the Component source directly taken from the component of Tridion UI.

<Content xmlns="Some Name space">
    <keywordlink xlink:href="tcm:202-9737-1024" xlink:title="Some Value"
        xmlns:xlink="http://www.w3.org/1999/xlink">Some Value</keywordlink>
</Content>

当我从模板构建器观察 tcm:Component 源时,我发现该字段没有属性.

When I observed the tcm:Component source from template builder, I observed that there are no attributes present for the field.

  <Content xmlns="Some Name space">
    <keywordlink>Some Value</keywordlink>
  </Content>

我想检索关键字的 Key 值.

I want to retrieve the Key value of the keyword.

我写了一个这样的 XSLT TBB.我正在使用 XSLT 中介器来执行 XSLT TBB.

I wrote a XSLT TBB like this. I am using XSLT mediator to execute XSLT TBBs.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:simple="Some Name space"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:tcm="http://www.tridion.com/ContentManager/5.0"
  xmlns:xh="http://www.w3.org/1999/xhtml" 
  xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:transform-ext="urn:tridion:transform-ext" 
  xmlns="http://www.w3.org/1999/xhtml" 
  exclude-result-prefixes="#default simple xh">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />

    <xsl:template match="/">
        <xsl:apply-templates 
            select="tcm:Component/tcm:Data/tcm:Content/simple:Content" />
    </xsl:template>

    <xsl:template match="simple:Content">
        <xsl:value-of  select="simple:keywordlink/@*"/>
        <xsl:value-of select=document(simple:keywordlink/@xlink:href)/>
    </xsl:template>
<xsl:stylesheet>

我得到空白输出.我想获取关键字的键值.

I am getting blank output. I want to get the key value of a keyword.

我得到空白输出,因为在 tcm:Component XML 中,没有属性.

I am getting blank output because in tcm:Component XML, there are no attributes.

我不确定如何导航到该关键字.

I am not sure how can I navigate to that keyword.

我应该检索 Key 的值,即它的 ID 是 123".

I should retrieve the value of Key i.e. "Its ID is 123".

有人可以帮忙吗?

推荐答案

仅使用 XSLT 中介器似乎不可能获得指向关键字字段中引用关键字的 xlink:href.

It seems it's impossible to get an xlink:href to the referred keyword in a Keyword field using just the XSLT Mediator.

为了克服这个问题,我创建了一个 .NET 化合物,它膨胀"了 XML 中的额外关键字信息.您必须将此组合放在 XSLT 组合之前.

To overcome this I created a .NET compound that "inflates" the extra keyword info in the XML. You'll have to place this compound just before the XSLT compound.

代码:

namespace ContentManagement.TBB.Templates
{
   [TcmTemplateTitle("Inflate Keyword Info")]
    public class GetExtendedComponent : TemplateBase
    {
       public override void Transform(Engine engine, Package package)
       {
           Initialize(engine, package);

           Component component = GetComponent();
           XmlElement componentXml = component.ToXml();

           XmlNamespaceManager ns = new XmlNamespaceManager(componentXml.OwnerDocument.NameTable);
           ns.AddNamespace("ns", component.Schema.NamespaceUri);

           ItemFields fields = new ItemFields(component.Content, component.Schema);
           InflateKeywords(fields, (XmlElement)componentXml.SelectSingleNode(String.Format("//ns:{0}", component.Schema.RootElementName), ns));

           ItemFields metaFields = new ItemFields(component.Metadata, component.MetadataSchema);
           InflateKeywords(metaFields, (XmlElement)componentXml.SelectSingleNode("//ns:Metadata", ns));

           Item xmlItem = package.CreateStringItem(ContentType.Component, componentXml.OuterXml);
           package.Remove(package.GetByName(Package.ComponentName));
           package.PushItem(Package.ComponentName, xmlItem);
       }

       private void InflateKeywords(ItemFields fields, XmlElement element)
       {
           XmlNamespaceManager ns = new XmlNamespaceManager(element.OwnerDocument.NameTable);
           ns.AddNamespace("ns", element.NamespaceURI);
           Logger.Debug("NS: " + element.NamespaceURI);
           foreach (ItemField field in fields)
           {
               if (field is KeywordField)
               {
                   KeywordField keywordField = (KeywordField)field;

                   XmlNodeList nodes = element.SelectNodes(String.Format("./ns:{0}", keywordField.Name), ns);
                   foreach (XmlNode node in nodes)
                   {
                       XmlElement kwelement = (XmlElement)node;

                       Logger.Debug(String.Format("Keyword titel: {0}", keywordField.Value.Title));
                       Logger.Debug(String.Format("Keyword Element Value: {0}", kwelement.InnerText));

                       kwelement.SetAttribute("href", "http://www.w3.org/1999/xlink", keywordField.Values.First(v => v.Title.Equals(kwelement.InnerText)).Id);
                       kwelement.SetAttribute("type", "http://www.w3.org/1999/xlink", "simple");
                       kwelement.SetAttribute("title", "http://www.w3.org/1999/xlink", kwelement.InnerText);
                   }
               }
               else if (field is EmbeddedSchemaField)
               {
                   EmbeddedSchemaField embedField = (EmbeddedSchemaField)field;

                   XmlNodeList nodes = element.SelectNodes(String.Format("./ns:{0}", embedField.Name), ns);
                   int i = 0; 
                   foreach (XmlNode node in nodes)
                   {
                       XmlElement embedElement = (XmlElement)node;
                       InflateKeywords(embedField.Values[i], embedElement);
                       i++;
                   }
               }

           }
       }
    }
}

这篇关于如何从 XSLT TBB 获取关键字的“键"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆