在Freemarker模板中访问带有命名空间的DOM元素

时间:2016-02-29 23:58:13

标签: dom freemarker

我有以下XML

<wmi xmlns="http://www.exmple.com/XMLSchema/fulfillment/v1/order/orderShipment" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/XMLSchema/fulfillment/v1/order/orderShipment OrderShipmentNotification.xsd">
<wmiHeader>
    <fileID>693401.20160229.130342.3541254</fileID>
    <version>2.0.0</version>
    <messageType>FSN</messageType>
    <genDate>2016-02-29T13:03:42Z</genDate>
    <from>
    </from>
</wmiHeader>
<orderShipNotification>
    <shipmentHeader dateTimeCreated="2016-02-29T13:03:42Z" requestNumber="2574445351883" />
    <shipmentDetails actualShipmentDateTime="2016-02-29T12:18:54Z" carrierCode="XX" carrierMethodCode="XX-01">
        <shipmentPackDetails trackingNumber="9361289672090007124848" trackingURL="https://example.com/go/TrackConfirmAction_input?qtc_tLabels1=323434">
            <shipmentPackLineDetails requestLineNumber="1" partnerItemID="FXT-CC-LB" itemQtyShipped="1" />
        </shipmentPackDetails>
    </shipmentDetails>
</orderShipNotification>
</wmi>

当我尝试访问时,我在Freemarker模板中收到错误。

${orderShipNotification.shipmentDetails.@actualShipmentDateTime[0]!""}

如果我从文档中删除命名空间,它工作正常。我从XML

中删除了以下内容
xmlns="http://www.exmple.com/XMLSchema/fulfillment/v1/order/orderShipment" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/XMLSchema/fulfillment/v1/order/orderShipment OrderShipmentNotification.xsd"

我做了一些调查。这是一个ftl指令。但目前尚不清楚这将如何解决问题。请告诉我如何访问这些属性。

 http://freemarker.incubator.apache.org/docs/ref_directive_ftl.html#ref.directive.ftl

1 个答案:

答案 0 :(得分:0)

使用

启动模板
<#ftl ns_prefixes={"D":"http://www.exmple.com/XMLSchema/fulfillment/v1/order/orderShipment"}>

这将命名空间设置为默认值(D代表默认值)。请注意,如果您还将使用XPath查询,则必须在元素名称之前写出D:(这是XPath限制)。

此处记录了这些内容:http://freemarker.org/docs/xgui_imperative_learn.html