5

私はSDLTridion2011SP1でTom.NetAPIに取り組んでいます。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

必要な変更を提案できますか。

ありがとうございました。

4

2 に答える 2

8

ItemField.Definitionは、フィールドコンテンツではなく、フィールドのスキーマ定義へのアクセスを提供するため、ExtensionXmlプロパティを使用してコンテンツにアクセスしないでください(そのため、コンテンツは空です)。このプロパティは、スキーマ定義に拡張データを格納するために使用されます。

XML / XHTMLコンテンツを含むフィールドを操作するには、コンポーネントのContentプロパティにアクセスするだけです。これは、すでにXmlElementであるためです。コンテンツの名前空間に注意する必要があるため、このXmlElementを照会するときはXmlNamespaceManagerを使用してください。たとえば、次の例では、「text」という名前のフィールドへの参照が提供されます。

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);
于 2012-05-10T10:16:33.890 に答える
2
textValuesss.Definition.ExtensionXml

これは間違ったプロパティです(定義はスキーマフィールド定義につながり、ExtensionXmlは拡張機能によって書き込まれたカスタムXMLデータ用です)。

代わりにtextValuesss.Valueを使用して、XMLとしてロードします。その後、XHTML名前空間を含む特定のXPathクエリでSelectSingleNodeを使用する必要があります。または、LinqtoXMLを使用して要素を検索します。

于 2012-05-10T10:01:54.007 に答える