You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XPath查询难题:如何按DPart区分获取cip4:Part的Separation属性

解决JDF XML中按DPart节点区分获取cip4:Part的Separation属性问题

问题背景

生成用于印刷工作流的JDF标准XML报告时,文档包含多个<DPart>节点,需要获取每个<DPart>下<cip4:Part>的Separation属性,但原有XPath查询无法正确区分不同<DPart>节点。

示例XML结构:

<DPart End="0" ID="0003" ParentRef="0002" Start="0">
  <DPM>
    <cip4:Root>
      <cip4:Intent cip4:ProductType="ProductPart"/>
      <cip4:Production>
        <cip4:Resource>
          <cip4:Part Separation="K1"/>
          <cip4:Color cip4:ActualColorName="Black" cip4:ColorType="Normal"/>
        </cip4:Resource>
        <cip4:Resource>
          <cip4:Part Separation="S1"/>
          <cip4:Color cip4:ActualColorName="Dieline" cip4:ColorType="Normal"/>
        </cip4:Resource>
        <cip4:Resource>
          <cip4:ColorantControl ColorantOrder="K1 S1" ColorantParams="K1 S1"/>
        </cip4:Resource>
        <cip4:Resource>
          <eg:InkCoverage>
            <eg:InkCov eg:Mm2="0.000000" eg:Pct="0.000000" eg:Separation="K1"/>
            <eg:InkCov eg:Mm2="182.337538" eg:Pct="0.721209" eg:Separation="S1"/>
          </eg:InkCoverage>
        </cip4:Resource>
      </cip4:Production>
    </cip4:Root>
  </DPM>
</DPart>

原查询/DPM[2]/*[name ()='cip4:Part']无效,原因是路径未关联<DPart>父节点,且命名空间处理存在问题。当前环境为Esko Automation Engine,支持XPath解析和JavaScript处理。


解决方案

1. 修正XPath查询

针对命名空间和路径问题,提供两种可靠的XPath写法:

方法1:绑定命名空间后查询

若Esko环境支持绑定命名空间(cip4标准命名空间为http://www.cip4.org/JDFSchema_1_1),可直接按节点名查询:

  • 获取所有<DPart>下的Separation属性:
    //DPart//cip4:Part/@Separation
    
  • 获取指定<DPart>(如第2个)下的Separation属性:
    //DPart[2]//cip4:Part/@Separation
    

方法2:忽略命名空间前缀,用local-name匹配

如果命名空间前缀存在不确定性,使用local-name()匹配节点名:

  • 获取所有<DPart>下的Separation属性:
    //DPart//*[local-name()='Part' and namespace-uri()='http://www.cip4.org/JDFSchema_1_1']/@Separation
    
  • 简化版(若无同名非cip4节点):
    //DPart//*[local-name()='Part']/@Separation
    

2. JavaScript遍历处理

在Esko Automation Engine中,可通过JavaScript结合XPath实现精细化处理,逐个<DPart>提取属性:

// 获取所有DPart节点集合
const dParts = document.evaluate("//DPart", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

// 遍历每个DPart
for (let i = 0; i < dParts.snapshotLength; i++) {
    const currentDPart = dParts.snapshotItem(i);
    // 在当前DPart下查找所有cip4:Part的Separation属性
    const sepAttrs = document.evaluate(
        ".//*[local-name()='Part']/@Separation",
        currentDPart,
        null,
        XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
        null
    );

    // 输出或处理当前DPart的Separation值
    console.log(`DPart ${i+1} 的Separation属性:`);
    for (let j = 0; j < sepAttrs.snapshotLength; j++) {
        const sepValue = sepAttrs.snapshotItem(j).value;
        console.log(`- ${sepValue}`);
        // 此处可将值写入报告或存储到变量
    }
}

内容的提问来源于stack exchange,提问作者Scott Haugaard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 20:35:29