【问题标题】:How can I get the 'key' of a keyword from an XSLT TBB?如何从 XSLT TBB 中获取关键字的“键”?
【发布时间】:2012-06-14 08:53:39
【问题描述】:

我正在研究 XSLT TBB(在 Tridion 2011 SP1 上使用 XSLT 中介器)以从关键字中检索键值。

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

Value: Some Value   
Key: Its ID is 123

这是一个普通的关键字。

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

组件 Source 如下所示: 这是直接取自 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 源时,我观察到该字段没有属性。

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

我要检索关键字的Key值。

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

<?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>

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

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

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

我应该检索 Key 的值,即“它的 ID 是 123”。

任何人都可以帮助如何做到这一点?

【问题讨论】:

  • 您似乎忘记提供源 XML 文档...在您的问题中提供的 XML 中没有像 tcm:Component/tcm:Data/tcm:Content 这样的东西。
  • 查看您的内容 XML,当您说“密钥”时,您指的是哪个属性?有一个 href 属性 (tcm:202-9737-1024) 和一个 title 属性。这些似乎都不符合您的期望。

标签: xslt tridion tridion-2011


【解决方案1】:

似乎不可能只使用 XSLT 中介器在关键字字段中获得指向关键字的 xlink:href。

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

代码:

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++;
                   }
               }

           }
       }
    }
}

【讨论】:

    【解决方案2】:

    以这种方式实现:-

    获取值字段:-

    document(simple:keywordlink/@xlink:href)/tcm:Keyword/tcm:Data/tcm:Title/text()
    
    <xsl:value-of select="document(simple:keywordlink/@xlink:href)/tcm:Keyword/tcm:Data/tcm:Title/text()" />
    

    结果:

    Some Value
    

    获取关键字段:-

    document(simple:keywordlink/@xlink:href)/tcm:Keyword/tcm:Data/tcm:Key/text()
    
    <xsl:value-of select="document(simple:keywordlink/@xlink:href)/tcm:Keyword/tcm:Data/tcm:Key/text()" />
    

    结果:

    Its ID is 123
    

    【讨论】:

      【解决方案3】:

      Keyword 的 Key 不存储在链接中(实际上只包含查找 Keyword 所需的最少信息)。因此,您必须加载关键字并从那里读取。

      Yoav 在这里展示了如何从 XSLT 中读取其他项目:

      http://yoavniran.wordpress.com/2009/07/11/implementing-the-xslt-mediator-part-1/

      这个 sn-p 似乎相关:

      <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>
      

      所以document() 调用会加载链接的项目(在本例中为多媒体组件),然后select 的其余部分会找到他们正在寻找的值。

      关键字 XML 如下所示:

      <?xml version="1.0"?>
      <tcm:Keyword xmlns:transform-ext="urn:tridion:transform-ext" 
          xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
          xmlns:xlink="http://www.w3.org/1999/xlink" 
          xmlns:tcm="http://www.tridion.com/ContentManager/5.0" 
          IsEditable="false" ID="tcm:1-233-1024">
          <tcm:Context>
              <tcm:Publication xlink:title="000 Parent Publication" xlink:href="tcm:0-1-1" 
                  xlink:type="simple"/>
              <tcm:OrganizationalItem xlink:title="Places" xlink:href="tcm:1-37-512" 
                  xlink:type="simple"/>
          </tcm:Context>
          <tcm:Info>
              <tcm:LocationInfo>
                  <tcm:WebDAVURL>/webdav/000%20Parent%20Publication/Places/New%20Keyword.tkw</tcm:WebDAVURL>
                  <tcm:Path>\000 Parent Publication\Places</tcm:Path>
              </tcm:LocationInfo>
              <tcm:BluePrintInfo>
                  <tcm:OwningPublication xlink:title="000 Parent Publication" xlink:href="tcm:0-1-1" xlink:type="simple"/>
                  <tcm:IsShared>false</tcm:IsShared>
                  <tcm:IsLocalized>false</tcm:IsLocalized>
              </tcm:BluePrintInfo>
              <tcm:VersionInfo>
                  <tcm:CreationDate>2012-06-11T09:09:03</tcm:CreationDate>
                  <tcm:RevisionDate>2012-06-11T09:09:03</tcm:RevisionDate>
                  <tcm:Creator xlink:title="TCMHOSTNAME\Administrator" 
                      xlink:href="tcm:0-11-65552" xlink:type="simple"/>
              </tcm:VersionInfo>
              <tcm:AllowedActions>
                  <tcm:Actions Managed="0" Deny="96" Allow="268560384"/>
              </tcm:AllowedActions>
          </tcm:Info>
          <tcm:Data>
              <tcm:Title>New Keyword</tcm:Title>
              <tcm:Description>New Keyword</tcm:Description>
              <tcm:Key>Key</tcm:Key>
              <tcm:IsAbstract>false</tcm:IsAbstract>
              <tcm:ParentKeywords/>
              <tcm:RelatedKeywords/>
              <tcm:MetadataSchema xlink:title="" xlink:href="tcm:0-0-0" xlink:type="simple"/>
              <tcm:Metadata/>
              <tcm:IsRoot>true</tcm:IsRoot>
          </tcm:Data>
      </tcm:Keyword>
      

      【讨论】:

      • 示例 XSLT 中的 document(simple:keywordlink/@xlink:href) 看起来是从链接的关键字中获取密钥的起点(对于纯 XSLT,我肯定会考虑方法)。我觉得这里真正的问题是“关键字的 XML 结构是什么?” ;)
      • @FrankvanPuffelen。直接取自 Tridion UI 的组件 XML 和取自模板生成器的 tcm:Component 源代码存在一些差异。在 tcm:Component source 我没有看到“keylink”的任何属性。你能帮我吗,我该如何解决我的问题。
      • 在这种情况下,您还可以通过 document('tcm:1-23') 加载组件并从那里获取 xlink:href。
      • 很抱歉恢复这个话题,但我遇到了同样的问题。通过 document() 获取组件不会获得额外的属性。有谁能够通过组件xml获取关键字uri?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-30
      相关资源
      最近更新 更多