是否可以提取 SDL Tridion 页面的信息,包括以 xml 格式关联的内容。
我期待类似这样的东西:
<page name="mypagename">
<page id="xxxxx"/>
<template name="abc" id="123">
<container name="xyz">
<content name="asd" id="123">
<Path="" \>
</content>
</container>
</template>
</page>
技术堆栈是 Tridion 2011 和 Java。
我是 Tridion 的新手,所以详细的回答会很有帮助。
最佳答案
根据您的实际需要,有很多更简单的方法可以做到这一点,但我使用以下用 C# 编写的 TBB 类构建一个页面 XML,根据组件模板元数据划分为内容区域
class GetPageXML : TemplateBase
{
public override void Transform(Engine engine, Package package)
{
Initialize(engine, package);
Logger.Debug("This will get the full page XML");
Item pageItem = m_Package.GetByType(ContentType.Page);
Page currentPage = GetPage();
m_Engine.PublishingContext.RenderContext.ContextVariables.Add("CURRENT_PAGE", currentPage);
XmlDocument pageXml = pageItem.GetAsXmlDocument();
foreach (XmlNode cpNode in pageXml.SelectNodes("/tcm:Page/tcm:Data/tcm:ComponentPresentations/tcm:ComponentPresentation", NSManager))
{
TcmUri componentURI = new TcmUri(cpNode.SelectSingleNode("tcm:Component/@xlink:href", NSManager).Value);
TcmUri componentTemplateURI = new TcmUri(cpNode.SelectSingleNode("tcm:ComponentTemplate/@xlink:href", NSManager).Value);
//Render the componentPresentation
XmlNode cpRenderElement = pageXml.CreateElement("tcm", "RenderedComponentPresentation", "http://www.tridion.com/ContentManager/5.0");
XmlAttribute attNamespace = pageXml.CreateAttribute("xmlns:tcdl");
attNamespace.Value = "http://www.tridion.com/ContentDelivery/5.3/TCDL";
cpRenderElement.Attributes.Append(attNamespace);
if (m_Engine.PublishingContext.RenderContext.ContextVariables.Contains("ORDINAL_POSITION"))
{
int ordinalPosition = ((int)m_Engine.PublishingContext.RenderContext.ContextVariables["ORDINAL_POSITION"]) + 1;
m_Engine.PublishingContext.RenderContext.ContextVariables["ORDINAL_POSITION"] = ordinalPosition;
// m_Engine.PublishingContext.RenderContext.ContextVariables["ORDINAL_POSITION"] = 0;
}
else
{
m_Engine.PublishingContext.RenderContext.ContextVariables.Add("ORDINAL_POSITION", 0);
}
//Get the metadata from the CT
ComponentTemplate componentTemplate = (ComponentTemplate)m_Engine.GetObject(componentTemplateURI);
String metadataXmlString = "";
String cpRegionName = "";
if (componentTemplate.Metadata != null)
{
metadataXmlString = componentTemplate.Metadata.OuterXml;
XmlNode cpRegionNode = componentTemplate.Metadata.SelectSingleNode("//*[local-name()='region']");
if (cpRegionNode != null)
{
cpRegionName = "_" + cpRegionNode.InnerText;
}
}
cpNode.SelectSingleNode("tcm:ComponentTemplate", NSManager).InnerXml = metadataXmlString;
String REGIONAL_POSITION = "REGIONAL_POSITION" + cpRegionName;
if (m_Engine.PublishingContext.RenderContext.ContextVariables.Contains(REGIONAL_POSITION))
{
int regionalPosition = ((int)m_Engine.PublishingContext.RenderContext.ContextVariables[REGIONAL_POSITION]) + 1;
m_Engine.PublishingContext.RenderContext.ContextVariables[REGIONAL_POSITION] = regionalPosition;
// m_Engine.PublishingContext.RenderContext.ContextVariables["ORDINAL_POSITION"] = 0;
}
else
{
m_Engine.PublishingContext.RenderContext.ContextVariables.Add(REGIONAL_POSITION, 0);
}
DateTime cpRenderStart = DateTime.Now;
string contentCP = m_Engine.RenderComponentPresentation(componentURI, componentTemplateURI);
DateTime cpRenderEnd = DateTime.Now;
TimeSpan cpRenderTime = cpRenderEnd.Subtract(cpRenderStart);
XmlAttribute attCPRenderTime = pageXml.CreateAttribute("RenderTime");
attCPRenderTime.Value = String.Format("{0:0000}", cpRenderTime.TotalMilliseconds) + " milliseconds";
cpNode.Attributes.Append(attCPRenderTime);
if (m_Engine.RenderMode != RenderMode.Publish)
{
//Remove the '&' symbols created for linking in previewmode
contentCP = contentCP.Replace("&", "");
}
try
{
cpRenderElement.InnerXml = contentCP;
}
catch (Exception e)
{
cpRenderElement.InnerXml = "Malformed XML in the output:" + e.Message;// +"<!--" + contentCP + "-->";
}
cpNode.AppendChild(cpRenderElement);
Component component = (Component)m_Engine.GetObject(componentURI);
XmlAttribute attComponentRevisionDate = pageXml.CreateAttribute("ModifiedOn");
attComponentRevisionDate.Value = component.RevisionDate.ToString("yyyy-MM-dd HH:mm:ss tt");
cpNode.Attributes.Append(attComponentRevisionDate);
//Get the metadata from the Component
metadataXmlString = "";
if (component.Metadata != null)
{
metadataXmlString = component.Metadata.OuterXml;
}
cpNode.SelectSingleNode("tcm:Component", NSManager).InnerXml = metadataXmlString;
}
//Add the rendered/published time
XmlAttribute attRenderTime = pageXml.CreateAttribute("RenderedAt");
attRenderTime.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss tt");
pageXml.DocumentElement.Attributes.Append(attRenderTime);
//Add PublishInfo
//Get the PublishTransaction
PublishTransaction pubTrans = GetPublishTransaction(engine);
if (pubTrans != null)
{
XmlElement nodePublisher = pageXml.CreateElement("Publisher");
XmlElement nodePublisherName = pageXml.CreateElement("name");
XmlElement nodePublisherId = pageXml.CreateElement("id");
XmlElement nodePublisherDescription = pageXml.CreateElement("description");
nodePublisherName.InnerText = pubTrans.Creator.Title;
nodePublisher.AppendChild(nodePublisherName);
nodePublisherId.InnerText = pubTrans.Creator.Id.ToString();
nodePublisher.AppendChild(nodePublisherId);
nodePublisherDescription.InnerText = pubTrans.Creator.Description;
nodePublisher.AppendChild(nodePublisherDescription);
pageXml.DocumentElement.AppendChild(nodePublisher);
}
//Add the target info
if (engine.PublishingContext.PublicationTarget != null)
{
XmlElement nodePublicationTarget = engine.PublishingContext.PublicationTarget.ToXml();
XmlNode nodePubs = nodePublicationTarget.SelectSingleNode("/tcm:PublicationTarget/tcm:Data/tcm:Publications", NSManager);
nodePubs.ParentNode.RemoveChild(nodePubs);
pageXml.DocumentElement.AppendChild(pageXml.ImportNode(nodePublicationTarget, true));
}
//Add the Page Modified time
ModificationInfo modInfo = GetNewestModificationDateFromPageXml(pageXml);
XmlAttribute attContentModifiedTime = pageXml.CreateAttribute("ContentLastModifiedOn");
attContentModifiedTime.Value = modInfo.ModificationDate.ToString("yyyy-MM-dd HH:mm:ss tt");
pageXml.DocumentElement.Attributes.Append(attContentModifiedTime);
XmlAttribute attLastModifiedItem = pageXml.CreateAttribute("LastModifiedItemURI");
attLastModifiedItem.Value = modInfo.ItemURI.ToString();
pageXml.DocumentElement.Attributes.Append(attLastModifiedItem);
XmlAttribute attLastModifiedTitle = pageXml.CreateAttribute("LastModifiedItemTitle");
attLastModifiedTitle.Value = modInfo.ItemTitle;
pageXml.DocumentElement.Attributes.Append(attLastModifiedTitle);
//Add PageTemplate Properties
XmlNode nodePageTemplate = pageXml.SelectSingleNode("/tcm:Page/tcm:Data/tcm:PageTemplate", NSManager);
TcmUri uriPageTemplate = new TcmUri(nodePageTemplate.Attributes["href", "http://www.w3.org/1999/xlink"].Value);
XmlElement pageTemplateXml = engine.GetObject(uriPageTemplate).ToXml(XmlSections.Data);
nodePageTemplate.ParentNode.ReplaceChild(pageXml.ImportNode(pageTemplateXml, true), nodePageTemplate);
//Add the publication properties
XmlNode nodePublication = pageXml.SelectSingleNode("/tcm:Page/tcm:Context/tcm:Publication", NSManager);
TcmUri uriPublication = new TcmUri(nodePublication.Attributes["href", "http://www.w3.org/1999/xlink"].Value);
XmlElement publicationXml = engine.GetObject(uriPublication).ToXml(XmlSections.Data);
nodePublication.ParentNode.ReplaceChild(pageXml.ImportNode(publicationXml,true), nodePublication);
m_Package.PushItem("UrbanCherryPageFrameWork", m_Package.CreateXmlDocumentItem(ContentType.Xml,pageXml));
}
您可以停在 XmlDocument pageXml = pageItem.GetAsXmlDocument(); 行,然后将该值插入包中并将其称为输出。
或者看看 DD4T模板,因为它们也为页面输出标准的 XML 结构。
关于xml - SDL Tridion : Logical structure of a page in XML format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15237296/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我想禁用HTTP参数的自动XML解析。但我发现命令仅适用于Rails2.x,它们都不适用于3.0:config.action_controller.param_parsers.deleteMime::XML(application.rb)ActionController::Base.param_parsers.deleteMime::XMLRails3.0中的等价物是什么? 最佳答案 根据CVE-2013-0156的最新安全公告你可以将它用于Rails3.0。3.1和3.2ActionDispatch::ParamsParser::
我正在遍历数组中的一组标签名称,我想使用构建器打印每个标签名称,而不是求助于“我认为:builder=Nokogiri::XML::Builder.newdo|xml|fortagintagsxml.tag!tag,somevalendend会这样做,但它只是创建名称为“tag”的标签,并将标签变量作为元素的文本值。有人可以帮忙吗?这个看起来应该比较简单,我刚刚在搜索引擎上找不到答案。我可能没有以正确的方式提问。 最佳答案 尝试以下操作。如果我没记错的话,我添加了一个根节点,因为Nokogiri需要一个。builder=Nokogi
这是一些奇怪的例子:#!/usr/bin/rubyrequire'rubygems'require'open-uri'require'nokogiri'print"withoutread:",Nokogiri(open('http://weblog.rubyonrails.org/')).class,"\n"print"withread:",Nokogiri(open('http://weblog.rubyonrails.org/').read).class,"\n"运行此返回:withoutread:Nokogiri::XML::Documentwithread:Nokogiri::
我正在尝试加载SAML协议(protocol)架构(具体来说:https://www.oasis-open.org/committees/download.php/3407/oasis-sstc-saml-schema-protocol-1.1.xsd),但在执行此操作之后:schema=Nokogiri::XML::Schema(File.read('saml11_schema.xsd'))我得到这个输出:Nokogiri::XML::SyntaxErrorException:Element'{http://www.w3.org/2001/XMLSchema}element',att
我正在尝试通过POST将XML内容发送到一个简单的Rails项目中的Controller(“解析”)方法(“索引”)。它不是RESTful,因为我的模型名称不同,比如“汽车”。我在有效的功能测试中有以下内容:deftest_index...data_file_path=File.dirname(__FILE__)+'/../../app/views/layouts/index.xml.erb'message=ERB.new(File.read(data_file_path))xml_result=message.result(binding)doc=REXML::Document.ne
我有这样的代码:@doc=Nokogiri::HTML(open(url)@doc.xpath(query).eachdo|html|putshtml#howgetcontentofanodeend我如何获取节点的内容而不是像这样: 最佳答案 这是READMEfile中的概要示例为Nokogiri展示了一种使用CSS、XPath或混合的方法:require'nokogiri'require'open-uri'#GetaNokogiri::HTML:Documentforthepagewe’reinterestedin...doc=N
恐怕我没有太多通过网络服务器发布文档(例如XML)的经验,所以如果我对HTTP的理解不足,我深表歉意。我在127.0.0.1上的ruby应用程序中设置了一个基本的MongrelWeb服务器端口2000.(服务器)。我在同一台计算机上运行一个单独的Ruby应用程序。(客户)。我需要客户端向服务器发送XML文档。我曾尝试使用Net::HTTP来执行此操作,但我找不到一个明确的示例来告诉我应该做什么。我试过了,但遇到了错误。我已将请求分解,使其尽可能基本:http=Net::HTTP.new("127.0.0.1",2000)http.post('file','query=foo')#x
我有一些XML文档,我想从Sinatra服务器获取这些文档。我做了一些搜索,但找不到任何具体的东西。我确实找到了构建器gem,但我不想从头开始构建文档。我试着做这样的事情get'/'xml='Myname90'bodyxmlend但这会在它周围添加HTML标签。这可能是我所缺少的非常基本的东西。你能给我指出正确的方向吗? 最佳答案 这对于Sinatra来说非常简单:get'/'docontent_type'text/xml'"Luis99"end在获取“/”时,响应将是XML"Luis99"使用正确的content_type。
我已经看到了几件事,但到目前为止似乎没有任何效果。我正在使用nokogirionrails3ruby1.9.2通过url解析xml。xml的片段如下所示:我正在尝试解析它以获取与NewsLineText关联的文本r=node.at_xpath('.//newslinetext')ifnode.at_xpath('.//newslinetext')s=node.at_xpath('.//newslinetext').textifnode.at_xpath('.//newslinetext')t=node.at_xpath('.//newslinetext').contentifnod