using xslt to create an xml file in c

2019-07-21 10:46发布

问题:

I have 5 different key/value pairs and I know how my xml file should look. How can I create an xml file out of it? 1) I can always do printf() - not preferable. 2) Can I use xslt (stylesheet (.xsl) file) to do that?

And, I also want to be able to do the reverse operation of what I just mentioned. Given an xml file, I want to extract those 5 key/value pairs. I guess, I would need xsl file for this operation for sure.

I just need a starting point in terms of whether is doable or not. I can code myself in C.

Sample xml file:

<element1 type="type1" name="value1">
  <start play="no"/>
  <element2 aaa="AAA"/>
  <element2 bbb="BBB"/>
  <element3 ccc="CCC">
     <element4/><!-- play="no"/>-->
  </element3>
</element1>

回答1:

Sounds like you're looking for libxml2, which allows you to read and write XML from C. It's very easy to use, and has several xml writer examples.

If you want to read XML in, take a look at their xml reader examples, or their parsing examples.

If you want to do XSLT from C, you can also use libxslt, which is built on top of libxml2. Although, if you want to read the values into variables in your code, you'd probably be better off just with the libxml2 parsers. XSLT is better for transforming an XML file into another XML file.



标签: c xml xslt libxml2