我正在研究XSLT TBB(使用Tridion 2011 SP1上的XSLT Mediator)从关键字中检索密钥值。
我的关键字看起来像这样。
Value: Some Value
Key: Its ID is 123
这是一个普通的关键字。
我创建了一个包含字段的模式。这些值将从List和Category中选择。
组件Source看起来像这样: 这是直接从Tridion UI组件中获取的Component源。
<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的值,即&#34;它的ID是123&#34;。
任何人都可以帮忙解决这个问题吗?
答案 0 :(得分:5)
似乎不可能只使用XSLT Mediator在关键字字段中获取xlink:href到refer关键字。
为了解决这个问题,我创建了一个.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++;
}
}
}
}
}
}
答案 1 :(得分:3)
关键字的密钥未存储在链接中(实际上只包含查找关键字所需的最少信息)。所以你必须加载关键字并从那里读取它。
Yoav在这里展示了如何从XSLT中读取其他项目:
http://yoavniran.wordpress.com/2009/07/11/implementing-the-xslt-mediator-part-1/
这段代码似乎很相关:
<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>
答案 2 :(得分:0)
以这种方式实施: -
获取“值”字段: -
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
获取Key字段: -
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