2016-07-21 9 views
1

xml_writeをストリームの代わりに変数に変換するにはどうすればよいですか?SWI-Prolog xml_write to variable

current_output(O), 
with_output_to(string(X), 
       xml_write(O,[element(table, [style="width:50%"], 
          [element(tr, [span = 2], 
          [element(td, [], ['First name']), 
          element(td, [], ['Last name'])])])], 
          [header(false)])). 

しかし、上記は、まだ出力ストリームに出力し、しかも、何でXを統一しない:

私はこれを試してみました。

<table style="width:50%"> 
    <tr span="2"> 
    <td>First name</td> 
    <td>Last name</td> 
    </tr> 
</table> 
O = <stream>(6D342F30), 
X = "". 

答えて

1

あなたはxml_write/3を使用しているシグネチャがあります:順序で

xml_write(+Stream, +Term, +Options) 

が効果的にストリームに書き込むためにここに出力されます。あなたではなく、変数と結果のXMLを統一するために探しているので、しかし、のように見えるxml_write/2を見てい:

xml_output(X) :- 
    table(Out), 
    with_output_to(string(X), 
        xml_write(Out,[header(false)]) 
       ), 
    writeln(X). 

table(R) :- 
    R = [element(table, [style="width:50%"], 
     [element(tr, [span = 2], 
     [element(td, [], ['First name']), 
     element(td, [], ['Last name'])])])]. 
:あなたのコードでこれを使用して

xml_write(+Data, +Options) is det 

を、それはのようになります

例クエリ:

?- xml_output(R). 
    <table style="width:50%"> 
    <tr span="2"> 
     <td>First name</td> 
     <td>Last name</td> 
    </tr> 
    </table> 
    R = "<table style=\"width:50%\">\n <tr span=\"2\">\n <td>First name</td>\n <td>Last name</td>\n </tr>\n</table>". 
+1

ありがとうございます!出来た! – Ash