【发布时间】:2013-02-04 12:00:40
【问题描述】:
我正在创建一个模板系统,该系统可以在客户端使用 Javascript 进行解释,以构建空白表单,例如给客户的信等。
我已经构建了模板并在伪代码中设置了逻辑,但是我对 jQuery 的不熟悉我可以使用一些方向来帮助我开始。
基本思想是在我的文本节点中有一个标记,它表示一个字段,例如${prologue} 然后将其添加到一个名为“fields”的数组中,然后该数组将用于在 xml 中搜索相应的节点名称。
XML
<?xml version="1.0" encoding="UTF-8"?>
<message>
<text>${Prologue} - Dear ${Title} ${Surname}. This is a message from FUBAR. An engineer called but was unable to gain access, a new appointment has been made for ${ProductName} with order number ${VOLNumber}, on ${AppointmentDate} between ${AppointmentSlot}.
Please ensure you are available at your premises for the engineer. If this is not convenient, go to fubar.com or call 124125121515 before 12:00 noon the day before your appointment. Please refer to your order confirmation for details on what will happen on the day. ${Epilogue} - Free text field for advisor input<
</text>
<inputTypes>
<textBox type="text" fixed="n" size="100" alt="Enter a value">
<Prologue size="200" value="BT ENG Appt Reschedule 254159" alt="Prologue field"></Prologue>
<Surname value="Hoskins"></Surname>
<ProductName value=""></ProductName>
<VOLNumber size="8" value="" ></VOLNumber>
<Epilogue value=""></Epilogue>
</textBox>
<date type="datePicker" fixed="n" size="8" alt="Select a suitable appointment date">
<AppointmentDate></AppointmentDate>
</date>
<select type="select" >
<Title alt="Select the customers title">
<values>
<Mr selected="true">Mr</Mr>
<Miss>Miss</Miss>
<Mrs>Mrs</Mrs>
<Dr>Dr</Dr>
<Sir>Sir</Sir>
</values>
</Title>
<AppointmentSlot alt="Select the appointment slot">
<values>
<Morning>9:30am - 12:00pm</Morning>
<Afternoon>1:00pm - 5:00pm</Afternoon>
<Evening>6:00pm - 9:00pm</Evening>
</values>
</AppointmentSlot>
</select>
</inputTypes>
</message>
伪代码
Get list of tags from text node and build array called "fields"
For each item in "fields" array:
Find node in xml that equals array item's name
Get attributes of that node
Jump to parent node
Get attributes of parent node
If attributes of parent node != child node then ignore
Else add the parent attributes to the result
Build html for field using all the data gathered from above
附录
这个逻辑可以吗,是否可以从节点的父节点开始向下导航?
关于继承,我们能否获得父属性,如果子属性不同,则将它们添加到结果中?如果 parent 中的属性数量不等于 child 中的数量怎么办?
请不要提供完全编码的解决方案,只是为了让我开始。
这是我目前从文本节点中提取标签的内容
//get value of node "text" in xml
var start = $(xml).find("text").text().indexOf('$');
var end = $(xml).find("text").text().indexOf('}');
var tag = "";
var inputType;
// find all tags and add them to a tag array
while (start >= 0)
{
//console.log("Reach In Loop " + start)
tag = theLetter.slice(start + 2, end);
tagArray.push(tag);
tagReplaceArray.push(theLetter.slice(start, end + 1));
start = theLetter.indexOf('$', start + 1);
end = theLetter.indexOf('}', end + 1);
}
欢迎任何其他建议或类似问题的链接。
谢谢!
【问题讨论】:
-
您控制 XML 生成吗?那是在其他一些过程中你可能会改变它吗?
-
不,假设我无法控制它
-
你为什么给“loosebruce”-1?在我发布此评论时,它已经是-4,没有任何解释。是否符合 StackOverflow 礼仪?
标签: javascript jquery xml xml-parsing