【问题标题】:Getting the complete XMLsource for a Xhtml field using Tom.Net APIs in SDL Tridion 2011 SP1在 SDL Tridion 2011 SP1 中使用 Tom.Net API 获取 Xhtml 字段的完整 XML 源
【发布时间】:2012-05-10 08:59:31
【问题描述】:

我正在研究 SDL Tridion 2011 SP1 中的 Tom.Net API。 我正在尝试检索 XhtmlField 的“源”部分。

我的来源是这样的。

<Content>
    <text>
        <p xmlns="http://www.w3.org/1999/xhtml">hello all<strong>
            <a id="ID1" href="#" name="ZZZ">Name</a>
        </strong></p>
    </text>
</Content>

我想获取这个“文本”字段的来源并处理名称为a的标签。

我尝试了以下操作:

ItemFields content = new ItemFields(sourcecomp.Content, sourcecomp.Schema);
XhtmlField textValuesss = (XhtmlField)content["text"]; 

XmlElement  textxmlelement = textValuesss.Definition.ExtensionXml;

Response.Write("<BR>" + "count:" + textxmlelement.ChildNodes.Count);
for (int i = 0; i < textxmlelement.ChildNodes.Count; i++)
{
    Response.Write("<BR>" + "nodes" + textxmlelement.ChildNodes[i].Name);
}

//get all the nodes with the name a
XmlNodeList nodeswithnameA = textxmlelement.GetElementsByTagName("a");
foreach (XmlNode eachNode in nodeswithnameA)
{
    //get the value at the attribute "id" of node "a"
    string value = eachNode.Attributes["id"].Value;
    Response.Write("<BR>" + "idValue" + value);
}

我没有得到任何输出。此外,我得到的计数为零。

我得到的输出:

计数:0

虽然我在该字段中有一些子标签,但我不明白为什么 0 会以 Count 出现。

任何人都可以建议所需的修改。

谢谢。

【问题讨论】:

    标签: tridion


    【解决方案1】:

    ItemField.Definition 允许访问字段的架构定义,而不是字段内容,因此您不应使用 ExtensionXml 属性来访问内容(这就是它为空的原因)。此属性用于在架构定义中存储扩展数据。

    要处理包含 XML/XHTML 内容的字段,我只需访问组件的 Content 属性,因为这已经是一个 XmlElement。您需要注意内容的命名空间,因此在查询此 XmlElement 时使用 XmlNamespaceManager。例如,以下将为您提供对名为“文本”的字段的引用:

    XmlNameTable nameTable = new NameTable();
    XmlNamespaceManager nsManager = new XmlNamespaceManager(nameTable);
    nsManager.AddNamespace("custom", sourceComp.Content.NamespaceURI);
    XmlElement fieldValue = (XmlElement)sourceComp.Content.SelectSingleNode(
                                    "/custom:Content/custom:text", nsManager);
    

    【讨论】:

      【解决方案2】:
      textValuesss.Definition.ExtensionXml
      

      这是错误的属性(定义导致 Schema 字段定义,而 ExtensionXml 用于由扩展编写的自定义 XML 数据)。

      您想改用 textValuesss.Value 并将其加载为 XML。之后,您可能应该将 SelectSingleNode 与包含 XHTML 名称空间的特定 XPath 查询一起使用。或者使用 Linq to XML 来查找元素。

      【讨论】:

      • 他正在搜索多个锚点,所以 .SelectNodes() 会更好
      • 确实如此。要点是使用 XPath 而不是在子节点上循环。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-14
      • 1970-01-01
      相关资源
      最近更新 更多