将Web表单数据映射到重复的XML元素

时间:2014-02-20 22:08:23

标签: xslt webforms xml-parsing xsd xml-serialization

我已经在网上寻找答案,并且无法在任何地方找到答案。

我正在尝试构建一个使用XSLT生成XML数据的网络表单。我基于其他形式做同样的事情,并且让小的测试结构工作正常。但是,当我尝试将其映射到重复元素时,我遇到了各种各样的问题。

以下是我所拥有的内容的简要概述。我有一个类似于此的XSLT文件(免责声明:为简洁而排除的行;此外,这些不是实际的字段名称):

<someForm:Name></someForm:Name>
<someForm:Address></someForm:Address>
<someForm:Plan>
    <someForm:Step></someForm:Step>
    <someForm:Description></someForm:Description>
</someform:Plan>

我还有一个关联的架构,如下所示:

<xs:element name="Name" />
<xs:element name="Address" />
<xs:element name="Plan" maxOccurs="10">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Step" />
            <xs:element name="Description" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

记下Plan元素中的“maxOccurs = '10'”!这是我遇到问题的地方!

我写了一个表格(同样,基于其他工作表格),如下所示:

<input type="text" id="getName" xpath="/*[local-name()='Name']">
<input type="text" id="getAddress" xpath="/*[local-name()='Address']">
<table>
    <tr><th>Step</th><th>Description</th></tr>
    <tr>
        <td><input type="text" id="getStep1" xpath="/*[local-name()='Plan']/*[local-name()='Step']"></td>
        <td><input type="text" id="getDesc1" xpath="/*[local-name()='Plan']/*[local-name()='Description']"></td>
    </tr>
</table>

这很好用;我能够提交数据,并根据需要创建XML。

但是,这是我的问题:我正在尝试编写我的设置,因此最多可以容纳十行(根据maxOccurs设置)。当我尝试这样做时它不起作用:

    <tr>
        <td><input type="text" id="getStep1" xpath="/*[local-name()='Plan']/*[local-name()='Step']"></td>
        <td><input type="text" id="getDesc1" xpath="/*[local-name()='Plan']/*[local-name()='Description']"></td>
    </tr>
    <tr>
        <td><input type="text" id="getStep2" xpath="/*[local-name()='Plan']/*[local-name()='Step']"></td>
        <td><input type="text" id="getDesc2" xpath="/*[local-name()='Plan']/*[local-name()='Description']"></td>
    </tr>

这给了我各种各样的问题,这取决于我如何调整它。有时,它不保存任何数据(对于步骤和描述);有时,它会为两行保存相同的数据。

我无法找到解释其工作原理的任何文档。我调整了XSLTHTMLschema,但都无济于事。这让我感到沮丧。

有没有人对如何解决这个问题有任何见解?

注意:这与我之前提到的另一个问题直接相关:Creating XSLT nodes dynamically/multiple XSLT nodes

提前感谢您的任何帮助。 。

2 个答案:

答案 0 :(得分:1)

以下是我最终如何使用它:

    <tr>
        <td><input type="text" id="getStep1" xpath="/*[local-name()='Plan'][1]/*[local-name()='Step']"></td>
        <td><input type="text" id="getDesc1" xpath="/*[local-name()='Plan'][1]/*[local-name()='Description']"></td>
    </tr>
    <tr>
        <td><input type="text" id="getStep2" xpath="/*[local-name()='Plan'][2]/*[local-name()='Step']"></td>
        <td><input type="text" id="getDesc2" xpath="/*[local-name()='Plan'][2]/*[local-name()='Description']"></td>
    </tr>

如果查看xpath,请注意“Plan”之后的[1]和[2]。当我将那些包含在xpath中时,它没有问题。

答案 1 :(得分:0)

您需要为每个 tr 指定所需的Plan元素,即第一个 tr 需要获取第一个Plan,依此类推。 为此,您可以使用[local-name()='Plan']谓词中的position()函数,例如:

[local-name()='Plan' and position()=1]

表示第一个 tr

[local-name()='Plan' and position()=2]    

表示第二个 tr ,依此类推。

这应该让你开始,但你也可以通过循环输出 tr 元素来使你的代码变得更聪明。

当您指定节点树(即节点集合)时,问题中的示例XPath将返回所有步骤或描述元素。位置功能允许您在节点树中选择单个节点。

相关问题