0

私は、2 つの Xml ドキュメントを分離し、それらのタグ コンテンツの一部をマージして、3 つ目の Xml ドキュメントを生成する C# アプリケーションに取り組んでいます。内部タグを含む 1 つのタグの値を抽出し、それを別のタグに転送する必要がある状況に直面しています。私はこのようなことを始めました:

var summaryElement = elementExternal.Element("summary");
var summaryValue = (string)summaryElement;
var summaryValueClean = ElementValueClean(summaryValue);

var result = new XElement("para", summaryValueClean)

ElementValueClean 関数が不要な空白を削除する場所。

これは、summary タグの値にテキストのみが含まれている場合に十分に機能します。摩擦は、概要タグに次のような子要素が含まれている場合に発生します。

<summary>
   Notifies the context that a new link exists between the <paramref name="source" /> and <paramref name="target" /> objects
   and that the link is represented via the source.<paramref name="sourceProperty" /> which is a collection.
   The context adds this link to the set of newly created links to be sent to
   the data service on the next call to SaveChanges().
</summary>

私はこのようなものを生産したいと思います:

<para>
Notifies the context that a new link exists between the <paramref name="source" /> and <paramref name="target" /> objects
and that the link is represented via the source.<paramref name="sourceProperty" /> which is a collection.
The context adds this link to the set of newly created links to be sent to
the data service on the next call to SaveChanges().
</para>

ソース タグのカタログ全体に表示される可能性のある埋め込みタグは、およそ 12 個あり、その内容を出力タグにマージする必要があります。したがって、一般化できる C# ソリューションが必要です。ただし、Xml フラグメントに適用して Xml フラグメントを生成できる Xslt 変換は、それが十分に単純であれば機能します。私の Xslt スキルは、使用されなくなったため減少しました。

4

1 に答える 1

1

関数を更新してElementValueClean()インライン ノードをサポートし、文字列値の代わりに Element を受け入れることができます。

foreach (XmlNode n in summaryElement.Nodes()) {
  if (node.NodeType == XmlNodeType.Text) {
      //do text cleanup
  }
  else n
}

要素を再ラップする XSLT は非常に単純ですが、使用可能な C# テキスト クリーンアップ ソリューションが既にあるため、C# ソリューションのほうが理にかなっていると思います。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:template match="summary">
        <para><xsl:apply-templates/></para>
    </xsl:template>

    <xsl:template match="node()|@*" priority="-1" mode="#default">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" mode="#current"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

または、テキストのクリーンアップを含め、すべてを XSLT で行うこともできます。その関数が何をするかは明確ではありませんが、XSLT で開始する方法は次のとおりです。

<xsl:template match="text()">
    <xsl:value-of select="normalize-space(.)"/>
</xsl:template>
于 2012-11-16T17:51:10.280 に答える