【发布时间】:2015-03-10 18:52:41
【问题描述】:
我有一个简单的 xml 文件,我想使用 XSLT 进行转换。
第一个节点是schema(header),后面的节点是实际数据。请注意,数据中有一些缺失的字段。
<ex:file xmlns:ex="http://example.com">
<ex:header>
<ex:title>houseNumber</ex:title>
<ex:title>street</ex:title>
<ex:title>city</ex:title>
<ex:title>state</ex:title>
</ex:header>
<ex:address>
<ex:field>108</ex:field>
<ex:field>Ridgewood Circle</ex:field>
<ex:field>Rochester</ex:field>
<ex:field>NY</ex:field>
</ex:address>
<ex:address>
<ex:field/>
<ex:field>W. Clark</ex:field>
<ex:field>Springfield</ex:field>
<ex:field>IL</ex:field>
</ex:address>
</ex:file>
我们希望以两种方式转换此 XML。 首先,我们要使用“标题”中的信息并替换四个 ex:field 元素,这样我可以得到如下所示的 XML:
<ex:file xmlns:ex="http://example.com">
<ex:header>
<ex:title>houseNumber</ex:title>
<ex:title>street</ex:title>
<ex:title>city</ex:title>
<ex:title>state</ex:title>
</ex:header>
<ex:address>
<ex:houseNumber>108</ex:houseNumber>
<ex:street>Ridgewood Circle</ex:street>
<ex:city>Rochester</ex:city>
<ex:state>NY</ex:state>
</ex:address>
<ex:address>
<ex:houseNumber/>
<ex:street>W. Clark</ex:street>
<ex:city>Springfield</ex:city>
<ex:state>IL</ex:state>
</ex:address>
</ex:file>
第二个变换是消除那些空元素:
<ex:file xmlns:ex="http://example.com">
<ex:header>
<ex:title>houseNumber</ex:title>
<ex:title>street</ex:title>
<ex:title>city</ex:title>
<ex:title>state</ex:title>
</ex:header>
<ex:address>
<ex:houseNumber>108</ex:houseNumber>
<ex:street>Ridgewood Circle</ex:street>
<ex:city>Rochester</ex:city>
<ex:state>NY</ex:state>
</ex:address>
<ex:address>
<ex:street>W. Clark</ex:street>
<ex:city>Springfield</ex:city>
<ex:state>IL</ex:state>
</ex:address>
</ex:file>
请注意,第二个“记录”中的“houseNumber”不见了。
我已经多次阅读 XSLT 教程 (http://www.w3schools.com/xsl/default.asp),但我不确定这是否真的可以做到。
在此先感谢大家。
【问题讨论】: