XMLwriter with prefix

如果使用xmlwriter 生成 xslt 文件。

sxlt 文件基本上都是手动书写。但有些时候, 需要程序自动生成次文件。 比较了StringBuilder, XElement 以及 XmlWriter。发现XmlWriter相对来讲, 比较方便。 如下demo:

 

代码
internal   const   string  XSLT_NAMESPACE  =   " http://www.w3.org/1999/XSL/Transform " ;
internal   const   string  SEARCH_RESULT_DEFAULT_NS  =   " http://www.aveva.com/VNET/eiwm " ;
internal   const   string  SEARCH_LIST_NS  =   " http://www.aveva.com/VNET/List " ;
        
private   void  WriteTemplateStart(XmlWriter writer)
        {
            writer.WriteStartDocument();

            
//  Write xsl:stylesheet need end
            writer.WriteStartElement( " xsl " " stylesheet " , XSLT_NAMESPACE);
            writer.WriteAttributeString(
" xmlns " " xsl " null , XSLT_NAMESPACE);
            writer.WriteAttributeString(
" xmlns " " v " null , SEARCH_RESULT_DEFAULT_NS);
            writer.WriteAttributeString(
" xmlns " " vl " null , SEARCH_LIST_NS);
            writer.WriteAttributeString(
" exclude-result-prefixes " " v vl " );
            writer.WriteAttributeString(
" version " " 1.0 " );

            
//  Write xsl:output
            writer.WriteStartElement( " xsl " " output " , XSLT_NAMESPACE);
            writer.WriteAttributeString(
" method " " xml " );
            writer.WriteAttributeString(
" indent " " yes " );
            writer.WriteEndElement();

            
//  write xsl:template
            writer.WriteStartElement( " xsl " " template " , XSLT_NAMESPACE);
            writer.WriteAttributeString(
" match " " vl:VNETList " );
            writer.WriteStartElement(
" xsl " " apply-templates " , XSLT_NAMESPACE);
            writer.WriteAttributeString(
" select " " v:Template " );
            writer.WriteEndElement();
            writer.WriteEndElement();

            
//  Write xsl:template need end
            writer.WriteStartElement( " xsl " " template " , XSLT_NAMESPACE);
            writer.WriteAttributeString(
" match " " v:Template " );
            
//  Write vl:VNETList need end
            writer.WriteStartElement( " vl " " VNETList " , SEARCH_LIST_NS);
            writer.WriteAttributeString(
" xmlns " "" null , SEARCH_RESULT_DEFAULT_NS);
            writer.WriteAttributeString(
" xmlns " " vl " null , SEARCH_LIST_NS);
            
//  Write Template need end
            writer.WriteStartElement( " Template " );
        }

 

 

基本上都是用了 “prefex”。 和一般的xml 没有很大的区别。

当然, 对有namespace的xml文件, 在使用xpath 的时候, 必须的把xpath 转化为含有namespace 的path,否则找不到对应的节点。转化函数如下:

 

代码
         public   string  TransferXpathToEIWMFormat( string  xpath)
        {
            
//  If the xpath contains 'v:' flag, remove them first. then add
            
//  together.
            xpath  =  xpath.Replace( " v: " string .Empty);

            
//  If the xpath end with '/'. removed.
             if  (xpath.EndsWith( " / " ))
                xpath 
=  xpath.Remove(xpath.Length  -   1 );

            Regex startedWithChar 
=   new  Regex( @" ^\w " );
            
if  (startedWithChar.IsMatch(xpath))
                xpath 
=   " ./ "   +  xpath;

            Regex splashRegex 
=   new  Regex( @" (/{1,2}) " );
            Regex elementRegex 
=   new  Regex( @" (\[)([^\@]\w*) " );
            Regex attributeRegex 
=   new  Regex( @" (\[\@) " );

            xpath 
=  splashRegex.Replace(xpath,  " $1v: " );
            
// xpath = attributeRegex.Replace(xpath, "$1v:");
            xpath  =  elementRegex.Replace(xpath,  " $1v:$2 "  );

            
return  xpath;
        }

 

 

 

你可能感兴趣的:(Writer)