2016-08-08 24 views
1

私のコンストラクタに名前空間を持つXDocumentを作成します。名前空間を使用するXElementを追加します。

this.nsXsl = XNamespace.Get("http://www.w3.org/1999/XSL/Transform"); 
this.doc = new XDocument(
       new XElement(nsXsl + "stylesheet", new XAttribute("version", "1.0"), new XAttribute(XNamespace.Xmlns + "xsl", "http://www.w3.org/1999/XSL/Transform"), 
       new XElement(nsXsl + "output", new XAttribute("method", "xml"), new XAttribute("indent", "yes"), new XAttribute("encoding", "UTF-8")), 
       new XElement(nsXsl + "strip-space", new XAttribute("elements", "*"))) 
       ); 

この文書は正しい構造を持ち、私が望むように見えます。

しかし、私のような機能を持っている:

private XElement createTemplate(string match, string node, string fork, string select) 
     { 
      return new XElement(this.nsXsl + "template", new XAttribute("match", match), new XElement(node, 
       new XElement(this.nsXsl + fork, new XAttribute("select", select)))); 

     } 

この関数は次のような構造のXElementオブジェクトを返します。

<template match="/shiporder/shipto" xmlns="http://www.w3.org/1999/XSL/Transform"> 
    <Line1> 
    <apply-templates select="city" xmlns="" /> 
    </Line1> 
</template> 

をしかし、私は同様のXElement必要があります。そこ

<xsl:template match="/shiporder/shipto"> 
    <Line1> 
    <xsl:apply-templates select="city" /> 
    </Line1> 
</xsl:template> 
+0

あなたが唯一の化粧品、そしてときに新しいのXElementがXDOCにどのように装着されている/されたいの変化? –

+0

@HenkHoltermanまでテンプレートのリストがあり、 'foreach(テンプレート内のXElement elem)のように追加します。 { doc.Add(elem); } ' – StellaMaris

+1

これで問題は解決します。何もしないでください。 –

答えて

1

を2点です。まず、Line1要素は名前空間で修飾されており、apply-templatesはそうではありませんが、逆は欲しいものです。それはあなた自身のことです:new XElement(this.nsXsl + node, …node、私が推測すると、"Line1")に名前空間を追加し、new XElement(fork, …(明らかに、fork"apply-templates"です)でそうすることを省略します。前者から後者へthis.nsXsl +を移動するだけです。

第2に、XSLT名前空間の接頭辞をxslとすることをお勧めします。接頭辞と名前空間の間のバインディングは、特別な形式の属性の形で表現された宣言によって設定されます。new XAttribute(XNamespace.Xmlns + prefix, namespaceUri)(実際には最初のコードスニペットで行います)。このバインディングは、宣言されている要素と他の宣言によってオーバーセットされていない限り、すべてのネストされた要素で有効です。 XMLライターは、名前空間で修飾された名前を持つ要素を発行すると、その名前空間が接頭辞にバインドされていることを検出し、その接頭辞を使用します(たとえば、最初のコードスニペットではhttp://www.w3.org/1999/XSL/Transformは接頭辞xslにバインドされます。すべてのネストされた要素の接頭辞)。 XMLライターは、使用されている名前空間がプレフィックスにバインドされていないことがわかった場合、デフォルトの名前空間宣言属性xmlns="namespace"を発行します(デフォルトの名前空間は属性にのみ影響し、属性には影響しません)。 XMLの情報モデルの観点から

、以下の三つのスニペットは等価です:

<template match="/shiporder/shipto" xmlns="http://www.w3.org/1999/XSL/Transform"> 
    <Line1 xmlns=""> 
    <apply-templates select="city" xmlns="http://www.w3.org/1999/XSL/Transform" /> 
    </Line1> 
</template> 

<xsl:template match="/shiporder/shipto" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <Line1> 
    <xsl:apply-templates select="city" /> 
    </Line1> 
</template> 

<weird:template match="/shiporder/shipto" xmlns:weird="http://www.w3.org/1999/XSL/Transform"> 
    <Line1> 
    <weird:apply-templates select="city" /> 
    </Line1> 
</template> 
+0

私は最初の点で同意します。私のタグ名の前に "xsl:"を追加すると述べた2番目の点は、新しいXAttribute(XNamespace.Xmlns + prefix、namespaceUri)をXElementに追加することによってのみ可能です。 @ステラマリス、正確に – StellaMaris

+0

。ちょうどあなたの最初のコードスニペットで行われたように。 – ach

+0

'XName nodeName = this.nsXsl +" template "を使用することは可能ですか? XElemnet e = this.doc.Element(nodeName); '何とか? – StellaMaris

関連する問題