Файл 1.xml
<?xml version="1.0" encoding="utf-8" ?> <root> <item id="1"> <code>001</code> <name>Node 1</name> </item> <item id="2"> <code>002</code> <name>Node 2</name> </item> </root>
-module(xml). -include_lib("xmerl/include/xmerl.hrl"). -import(xmerl_xs, [xslapply/2, value_of/1, select/2, built_in_rules/2 ]). -export([process_to_file/0]). process_to_file() -> process_to_file('1.xml'). process_to_file(XMLDoc) -> {XMLContent,_} = xmerl_scan:file(XMLDoc, [{space, normalize}]), TransformedXML = template(XMLContent), io:format("~s", [TransformedXML]). %%% templates template(E = #xmlElement{name='root'}) -> #xmlObj{type=_, value=Val} = select("count(item)", E), ["count: ", io_lib:format("~p", [Val]), "\n", xslapply(fun template/1, select("item", E))]; template(E = #xmlElement{name='item'}) -> ["id: ", xslapply(fun template/1, select("@id", E)), " code: ", value_of(select("code", E)), " name: ", value_of(select("name", E)), "\n"]; template(E) -> built_in_rules(fun template/1, E).
erl
c("xml.erl").
або c(xml).
xml:process_to_file().
value_of(E) -> List
<xsl:template match="title"> <div align="center"> <h1><xsl:value-of select="." /></h1> </div> </xsl:template>
template(E = #xmlElement{name='title'}) -> ["<div align="center"><h1>", value_of(select(".", E)), "</h1></div>"]
xslapply(Fun::Function, EList::list()) -> List
<xsl:template match="doc/title"> <h1> <xsl:apply-templates/> </h1> </xsl:template>
template(E = #xmlElement{ parents=[{'doc',_}|_], name='title'}) -> ["<h1>", xslapply(fun template/1, E), "</h1>"];
© accounting.org.ua - 2024