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

Accessing DOM element with namespace in Freemarker template

我有以下 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

使用

启动模板
<#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