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

HL7 V2 ADT_A01 v3消息XML一致性校验报错求助

HL7 V2.3 ADT_A01 Message Validation Errors (MSH-1, MSH-2, MSH-9 Length Issues)

我正在使用HAPI库编写Java程序,基于一致性XML Profile校验HL7 V2.3的ADT_A01消息,但校验时出现了几个长度相关的错误,导致开发无法推进。以下是我的代码、输入消息和控制台错误输出:

Java校验代码

public static void main(String[] args) throws Exception {
    try {
        // Load the conformance profile
        ProfileParser ourProfileParser = new ProfileParser(false);
        RuntimeProfile ourConformanceProfile = ourProfileParser.parseClasspath("A01_v23.xml");
        //Read a test non-conformant HL7 from file. See my GitHub page for this file
        String message = readHl7FileDataAsString("C:\\Jai\\ADT\\ADT_A01_v2.5_Conformance_XML\\A01_v23.txt");
        PipeParser parser=new PipeParser();
        System.out.println("parser configuration "+parser.getParserConfiguration().toString());
        //parse the HL7 message from the file data
        Message msg = parser.parse(message);
        // Validate the HL7 message using the sample HL7 conformance profile I have provided. See my GitHub page for the XML file
        HL7Exception[] errors = new DefaultValidator().validate(msg, ourConformanceProfile.getMessage());
        // Display all the validation errors that are generated.
        System.out.println("The following validation errors were found during message validation:");
        for (HL7Exception hl7Exception : errors) {
            System.out.println(hl7Exception);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

输入HL7消息

MSH|^~\&|NSI^UID^D|A^B^C|RID^UID^h|hm|201907071123||ADT^A01^ADT_A01|MSGA01023|P|2.3
EVN|A01|201907071153|201908081153|02|11|20190708105155
PID|||HO2^^^A^MR||Ada^Pal^Kin^Jr^Mr||196101011153|M|Aam^Pau^Kin^Jr^Mr^MA^Ali^AL|Bl|St^^Blr^Ka^56^Ind^Off|91|^Off^Ph^^91^80^123^567^PHo|^91^Ph^d^91^^080^767^P1|Eng^Hin|M|Hin|124|SSN503|DL3|I|For|Bir|Y|2|C||91|201801012022|Y
PV1||I|6N^01^B23^ICU||||010^ATT^AON^A|018^ATT^AAR^A|||||||||014^END^RON^A|S|100|A|||||||||||||||||ISH^200701102300||GL|||||201907071153|201907080500

控制台错误输出

The following validation errors were found during message validation:
ca.uhn.hl7v2.conf.check.ProfileNotFollowedException: The type Field Separator has length 3 which exceeds max of 1 at MSH-1(0)
ca.uhn.hl7v2.conf.check.ProfileNotFollowedException: The type Encoding Characters has length 12 which exceeds max of 4 at MSH-2(0)
ca.uhn.hl7v2.conf.check.ProfileNotFollowedException: The type Message Type has length 15 which exceeds max of 9 at MSH-9(0)

解决方案

1. 修复MSH段的HTML转义错误

你的输入消息中错误地将HL7编码字符里的&写成了HTML转义格式的&,这直接破坏了MSH-1和MSH-2的格式:

  • 错误原因:HL7消息的MSH-2字段要求是4个编码字符(^~\&),但转义后的^~\&长度为12,同时解析器错误识别了MSH-1的分隔符,导致长度校验失败。
  • 修正方法:将消息中的&替换为原始的&,修正后的MSH行如下:
    MSH|^~\&|NSI^UID^D|A^B^C|RID^UID^h|hm|201907071123||ADT^A01^ADT_A01|MSGA01023|P|2.3
    
    修正后,MSH-1为单个|(符合长度1的要求),MSH-2为^~\&(符合长度4的要求),前两个错误会消失。

2. 解决MSH-9长度超限问题

错误提示MSH-9的长度为15,超过了Profile定义的最大长度9。这里有两种可行的处理方式:

方式一:简化MSH-9字段

HL7 V2.3中MSH-9的第三个组件(Message Structure)是可选的,你可以移除它,将MSH-9改为ADT^A01,此时长度为7,符合max 9的限制:
修正后的MSH行片段:

...||ADT^A01|MSGA01023|P|2.3

方式二:更新一致性XML Profile

如果业务需要保留MSH-9的三个组件,需要修改A01_v23.xml中对MSH-9的长度限制:

  • 可以将MSH-9的整体最大长度调整为15;
  • 或者更精准地为每个组件设置长度限制(比如第一个组件max 3,第二个max 3,第三个max 7,加上分隔符总长度为15)。

3. 验证修正结果

将修正后的消息替换原输入文件,重新运行校验程序,这三个错误应该会被解决。如果还有其他错误,再针对性调整消息或Profile。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:40:15