c# - Add XElement that uses namespace -


i create xdocument namespaces in constructor e.g.

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", "*")))                ); 

this document has right structure , looks want.

but have function like:

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))));          } 

this function returns xelement of following structure:

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

but need xelement like:

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

there 2 points. first, line1 element qualified namespace , apply-templates not, while want reverse. that's own doing: add namespace in new xelement(this.nsxsl + node, … (node, presume, "line1"), , omit in new xelement(fork, … (obviously, fork "apply-templates"). move this.nsxsl + former latter spot.

second, you'd prefer xslt namespace denoted prefix, xsl. binding between prefix , namespace set declaration expressed in form of attribute of special form, new xattribute(xnamespace.xmlns + prefix, namespaceuri) (you in first code snippet). binding valid in element declared , nested elements, unless overset declaration. when xml writer emits element namespace-qualified name, detects that namespace bound prefix , uses prefix (e. g., in first code snippet, namespace http://www.w3.org/1999/xsl/transform bound prefix xsl, xml writer adds xsl: prefix nested elements). if xml writer finds out used namespace not bound prefix, emits default namespace declaration attribute, xmlns="namespace" (note default namespace affects elements, not attributes).

from viewpoint of xml information model, following 3 snippets equivalent:

<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> 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -