针对xslt一些见解

         最近刚好有个项目关于xslt解释相关,需要根据一个数据源xml和目标xml文件根据用户的某种需求生成相应的xslt文件。

</pre><h2>    一,xslt解释库选择</h2><div><span style="font-size: 18px;">     1, msxml <span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px">微软的xml语言解析器,用来解释xml语言的,<span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; line-height:24px; text-indent:28px">支持解释xpath,更快的分析器和XSLT引擎,对XML流更好的处理,更好的一致性支持。</span></span></span></div><div><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; line-height:24px; text-indent:28px">      2,Xalan <span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px">它是一个XSLT的处理包,用于将XML文档转为HTML文档,文本文件TXT,或其他格式的XML文档。它实现了XSLT和XPath的接口,现在也有c/c++版本了。</span></span></span></span></div><div><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; line-height:24px; text-indent:28px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px">      3,libxslt这也是很好的c/c++的xml解释器,至此xpath/xslt的解释。</span></span></span></span></div><div><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; line-height:24px; text-indent:28px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px">  </span></span></span></span></div><div><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; line-height:24px; text-indent:28px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px">      对于以上3个解释器,第一个msxml是微软自己的东西,更新的比较慢,但是如果是在windows下的话,用起来还不错。后面两个xalan和libxslt是开源的,支持跨平台。windows/linux,本人下载过xalan,先发编译不过,然后弃用了,在windows下用了msxml.这个库在网上对应的资料都比较老。大多都是vc6.0时期的。</span></span></span></span></div><div><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; line-height:24px; text-indent:28px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"></span></span></span></span></div><h2><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; line-height:24px; text-indent:28px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px">  二,使用场景</span></span></span></span></h2><div><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; line-height:24px; text-indent:28px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"></span></span></span></span></div><div><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; line-height:24px; text-indent:28px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"></span></span></span></span></div><div><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; line-height:24px; text-indent:28px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px">   <img src="" alt="" /><img src="https://img-blog.csdn.net/20140702173718906?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHZsdmZ1bmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></span></span></span></span></div><div><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; line-height:24px; text-indent:28px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px">    如上图所示:这是在Altova Mapforce通过多两个xsd文件相互关联,左边对应的数据源xml命名为ExpReport.xml;右边的目标文件源对应的xml命名<span style="color:rgb(51,51,51); font-family:arial; font-size:18px; line-height:20.020000457763672px; text-indent:28px">ExpReport-Target,则</span>生成对应的xslt文件。</span></span></span></span></div><div><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; line-height:24px; text-indent:28px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px">    结果如此:</span></span></span></span></div><div><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; line-height:24px; text-indent:28px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px">    <img src="" alt="" /></span></span></span></span></div><div><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; line-height:24px; text-indent:28px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"></span></span></span></span></div><div><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; line-height:24px; text-indent:28px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px">   把此文件命名为<span style="color:rgb(51,51,51); font-family:arial; font-size:18px; line-height:20.020000457763672px; text-indent:28px">ExpReport.xslt,在这里可以看见xslt里面语法结构,其实就是类似的css一样,满足了各种逻辑操作,具体可见W3c官网。</span></span></span></span></span></div><div><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; line-height:24px; text-indent:28px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"><span style="color:rgb(51,51,51); font-family:arial; font-size:18px; line-height:20.020000457763672px; text-indent:28px">    然则我们可以通过xslt把源文件xml 转化为预期想要的结果,可以转为text,xhtml,html   </span></span></span></span></span></div><div><span style="font-size:18px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"><span style="color:rgb(51,51,51); font-family:arial,宋体,sans-serif; line-height:24px; text-indent:28px"><span style="color:rgb(51,51,51); font-family:arial; line-height:20.020000457763672px"><span style="color:rgb(51,51,51); font-family:arial; font-size:18px; line-height:20.020000457763672px; text-indent:28px">    以下c++实现其转换代码                                                                                                                                                                                                        </span></span></span></span></span><pre code_snippet_id="414144" snippet_file_name="blog_20140702_1_6061955" name="code" class="cpp">void CmyXsltParaDlg::OnBnClickedButton3()
{
	// TODO: 在此添加控件通知处理程序代码
	CoInitialize(NULL);	

	variant_t vResult;
	variant_t vResult1;
	
    MSXML2::IXMLDOMDocumentPtr pXml(MSXML2::CLSID_DOMDocument);
    MSXML2::IXMLDOMDocumentPtr pXslt (MSXML2::CLSID_DOMDocument);

	try{
		CString strxml ;
		CString strxsl;
		m_editxml.GetWindowTextW(strxml);
		m_editxsl.GetWindowTextW(strxsl);
		vResult = pXml->load(_bstr_t(strxml));
		vResult = pXslt->load(_bstr_t(strxsl));
		_bstr_t str = pXml->transformNode(pXslt);
		CString strr = str;
		m_edittransform.SetWindowText(strr);


	} catch(_com_error &e) {		
		printf("Error loading XML files : %s\n",(const char*)_bstr_t(e.Description()));	
		exit(-1);
	}

}
          

   通过msxml转化后得出out为:
    
     
<?xml version="1.0" encoding="UTF-8"?>
<Company xmlns="http://my-company.com/namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://my-company.com/namespace D:/Example1/ExpReport-Target.xsd">
	<Employee>
		<Title>Ben</Title>
	</Employee>
</Company>

   









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值