gpt4 book ai didi

Tridion Core 服务 - 使用分层分类法

转载 作者:行者123 更新时间:2023-12-02 16:39:40 26 4
gpt4 key购买 nike

我正在使用 Tridion 核心服务 (Tridion 2011 SP1) 检索给定类别 ID 的关键字列表。

CoreService2010Client client = new CoreService2010Client();   
XElement xmlCategoryKeywords = client.GetListXml(category.Id,
new KeywordsFilterData());

这会返回一个扁平的 XML 结构,表示我们的分类法,深度为 4 层。

文档详细介绍了处理此问题的方法:

var categoryKeywords = xmlCategoryKeywords.Elements().Select(element => 
element.Attribute("ID").Value).Select(id => (KeywordData)client.Read(id, null)
);
foreach (KeywordData keyword in categoryKeywords)
{
Console.WriteLine("\t Keyword ID={0}, Title={1}", keyword.Id, keyword.Title);
}

但是,这只会列出每个关键字。 KeywordData 对象包含属性 ParentKeywords,因此可以在内存中构建层次结构。

是否可以从具有分层结构的核心服务中检索 XML?或者更简单的方法来处理这些数据?

最佳答案

一种方法是使用TaxonomiesOwlFilterData:

string publicationId = "tcm:0-3-1";
var filter = new TaxonomiesOwlFilterData();
filter.RootCategories = new[] {new LinkToCategoryData{ IdRef = "tcm:3-158-512"},};
var list = ClientAdmin.GetListXml(publicationId, filter);

正如您所看到的,它在发布时被调用,但您可以将其范围缩小到一个或多个类别。它会返回可怕的 XML 列表,您可以像这样进一步处理:

XNamespace tcmc = publicationId + "/Categories#";
XNamespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
XNamespace tcmt = "http://www.tridion.com/ContentManager/5.2/Taxonomies#";

var taxonomyTree = new Dictionary<string, List<string>>();
var keywordNodes = list.Descendants(tcmc + "cat");
foreach (var keywordNode in keywordNodes)
{
var parents = new List<string>();
var parentNodes = keywordNode.Descendants(tcmt + "parentKeyword");
if (parentNodes.Count() > 0)
{
foreach (var parentNode in parentNodes)
{
parents.Add(parentNode.Attribute(rdf + "resource").Value);
}
}
taxonomyTree.Add(keywordNode.Attribute(rdf + "about").Value, parents);
}

因此,您将获得关键字和相应父关键字的无序列表,您可以根据需要进一步处理它们。没有父项的项显然是父关键字。它可能不是最漂亮的解决方案,但至少您只需要一次对服务器的调用,而不必读取每个关键字。

关于Tridion Core 服务 - 使用分层分类法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9530324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com