使用for-each进行XSLT验证

时间:2014-02-03 14:18:43

标签: xml xslt

我正在尝试为XSLT验证模板编写逻辑,以检查XML消息以查找各种条件的重复循环。

消息的结构是

<VXU_V04.ORDER> One or more may be present, not a required group; I’m calling this “OrderGroup”
<ORC> Required; only one ORC Segment per OrderGroup allowed
<ORC.3> Required; only one ORC.3 field per ORC Segment allowed
<EI.1>, <EI.2>, <EI.3> and <EI.4> are sub-components of ORC.3

这四个字段都有我自己需要编码的验证。每个ORC段只有一个ORC.3,每个ORC只有一组EI子组件。但是,我仍然会在订单组中考虑多个ORC段的概念,因此我仍然需要使用for-each构造来考虑多个ORC段。

这是我的ORC.3验证模板:

<xsl:stylesheet version="2.0" xmlns:a="urn:hl7-org:v2xml" xmlns:dil="http://www.aegis.net/msg/filter/jaxb/rule" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!--      
         Must be VALUED AND 
         Must not repeat
         -->
    <xsl:variable name="OrderGroup" select="/a:VXU_V04/a:VXU_V04.ORDER"/>
    <xsl:variable name="ORCSegment" select="/a:VXU_V04/a:VXU_V04.ORDER/a:ORC"/>
    <xsl:variable name="ORC3Field" select="/a:VXU_V04/a:VXU_V04.ORDER/a:ORC/a:ORC.3"/>
    <xsl:variable name="EI1Field" select="/a:VXU_V04/a:VXU_V04.ORDER/a:ORC/a:ORC.3/a:EI.1"/>
    <xsl:variable name="EI2Field" select="/a:VXU_V04/a:VXU_V04.ORDER/a:ORC/a:ORC.3/a:EI.2"/>
    <xsl:variable name="EI3Field" select="/a:VXU_V04/a:VXU_V04.ORDER/a:ORC/a:ORC.3/a:EI.3"/>
    <xsl:variable name="EI4Field" select="/a:VXU_V04/a:VXU_V04.ORDER/a:ORC/a:ORC.3/a:EI.4"/>
    <xsl:template match="/">
        <dil:filterruleresult>
             <xsl:choose>
                 <xsl:when test= "count($OrderGroup)= 0">
                    <dil:message type="info">VXU Order Group not found.</dil:message>
                 </xsl:when>
             </xsl:choose>
             <xsl:for-each select="$OrderGroup">
                     <xsl:choose>
                       <xsl:when test="count(a:ORC)= 0">  
                          <dil:message type="error">When Order Group present, ORC Segment is required.</dil:message>
                       </xsl:when>
                       <xsl:when test="count(a:ORC)> 1">  
                        <dil:message type="error">Only one Order Segment (ORC) allowed per Order Group.</dil:message>
                       </xsl:when>
                       <xsl:otherwise>
                          <xsl:for-each select="a:ORC">
                             <xsl:choose>
                                 <xsl:when test="count(a:ORC.3)= 0">
                                    <dil:message type="error">ORC-3 Filler Order Number not found but is required.</dil:message>
                                 </xsl:when> 
                                 <xsl:when test="count(a:ORC.3)> 1">
                                     <dil:message type="error">Only one Filler Order Number (ORC-3) allowed per Order Segment.</dil:message>
                                 </xsl:when> 
                                 <xsl:when test="count(a:EI.1)> 1">
                                     <dil:message type="error">When present, only one Filler Order Number (ORC-3) allowed per Order Segment.</dil:message>
                                 </xsl:when> 
                                 <xsl:when test="count(a:EI.1)= 0">
                                     <dil:message type="error">When Filler Order Number Present, Entity Identifier (ORC-3/EI-1) is required.</dil:message>
                                 </xsl:when>
                                  <xsl:when test="count(a:EI.3)= 0 and count(a:EI.2)= 0">
                                     <dil:message type="error">When Filler Order Number Present, Namespace ID (ORC-3/EI-2) or Universal ID (ORC-3/EI-3) is required.</dil:message>
                                 </xsl:when>  
                                 <xsl:when test="count(a:EI.3)= 1 and count(a:EI.4)= 0">
                     <dil:message type="error">When Filler Order Number Universal ID is present, Universal ID Type (ORC-3/EI-4) is required.</dil:message>
                                 </xsl:when>
                                 <xsl:when test="count(a:EI.3)= 0 and count(a:EI.4)= 0">
                     <dil:message type="error">When Filler Order Number Universal ID is not present, Universal ID Type (ORC-3/EI-4) is not supported.</dil:message>
                                 </xsl:when> 
                                 <xsl:when test="count(a:ORC.3)= 1">
                                     <dil:message type="info">Value of '<xsl:value-of select="./a:EI.1"/>', '<xsl:value-of select="./a:EI.2"/>', '<xsl:value-of select="./a:EI.3"/>', '<xsl:value-of select="./a:EI.4"/>' found for Filler Order Number (ORC-2).</dil:message>
                                 </xsl:when>
                             </xsl:choose>
                          </xsl:for-each>  
                       </xsl:otherwise>
                      </xsl:choose> 
             </xsl:for-each>      
        </dil:filterruleresult>
    </xsl:template>
</xsl:stylesheet>

问题是模板根本找不到我的EI字段。我一直遇到错误“当填充订单号存在时,实体标识符(ORC-3 / EI-1)是必需的。”但我的测试信息中确实有EI-1值。

以下是我的测试信息的相关部分:

  <VXU_V04.ORDER>
    <ORC>
      <ORC.1>RE</ORC.1>
      <ORC.3>
        <EI.1>197023</EI.1>
        <EI.2>DCS</EI.2>
      </ORC.3>
      <ORC.10>
        <XCN.2>
          <FN.1>Clerk</FN.1>
        </XCN.2>
        <XCN.3>Myron</XCN.3>
      </ORC.10>
      <ORC.17>
        <CE.1>DCS</CE.1>
        <CE.2>Dabig Clinical System</CE.2>
        <CE.3>StateIIS</CE.3>
      </ORC.17>
    </ORC>
    <RXA>
      <RXA.1>0</RXA.1>
      <RXA.2>1</RXA.2>
      <RXA.3>
        <TS.1>20090415132511</TS.1>
      </RXA.3>
      <RXA.4>
        <TS.1>20090415132511</TS.1>
      </RXA.4>
      <RXA.5>
        <CE.1>31</CE.1>
        <CE.2>Hep B Peds NOS</CE.2>
        <CE.3>CVX</CE.3>
      </RXA.5>
      <RXA.6>999</RXA.6>
      <RXA.9>
        <CE.1>01</CE.1>
        <CE.2>historical record</CE.2>
        <CE.3>NIP0001</CE.3>
      </RXA.9>
    </RXA>
  </VXU_V04.ORDER>
  <VXU_V04.ORDER>
    <ORC>
      <ORC.1>RE</ORC.1>
      <ORC.3>
        <EI.1>197027</EI.1>
        <EI.2>DCS</EI.2>
      </ORC.3>
      <ORC.10>
        <XCN.2>
          <FN.1>Clerk</FN.1>
        </XCN.2>
        <XCN.3>Myron</XCN.3>
      </ORC.10>
      <ORC.12>
        <XCN.2>
          <FN.1>Pediatric</FN.1>
        </XCN.2>
        <XCN.3>MARY</XCN.3>
        <XCN.10>L</XCN.10>
        <XCN.21>MD</XCN.21>
      </ORC.12>
    </ORC>
    <RXA>
      <RXA.1>0</RXA.1>
      <RXA.2>1</RXA.2>
      <RXA.3>
        <TS.1>20090531132511</TS.1>
      </RXA.3>
      <RXA.4>
        <TS.1>20090531132511</TS.1>
      </RXA.4>
      <RXA.5>
        <CE.1>48</CE.1>
        <CE.2>HIB PRP-T</CE.2>
        <CE.3>CVX</CE.3>
      </RXA.5>
      <RXA.6>999</RXA.6>
      <RXA.9>
        <CE.1>00</CE.1>
        <CE.2>new Immunization record</CE.2>
        <CE.3>NIP0001</CE.3>
      </RXA.9>
      <RXA.10>
        <XCN.2>
          <FN.1>Sticker</FN.1>
        </XCN.2>
        <XCN.3>Nurse</XCN.3>
      </RXA.10>
      <RXA.11>
        <LA2.4>
          <HD.1>DCS_DC</HD.1>
        </LA2.4>
      </RXA.11>
      <RXA.15>33k2a</RXA.15>
      <RXA.17>
        <CE.1>PMC</CE.1>
        <CE.2>sanofi</CE.2>
        <CE.3>MVX</CE.3>
      </RXA.17>
    </RXA>
    <RXR>
      <RXR.1>
        <CE.1>C28161</CE.1>
        <CE.2>IM</CE.2>
        <CE.3>NCIT</CE.3>
        <CE.4>IM</CE.4>
        <CE.5>IM</CE.5>
        <CE.6>HL70162</CE.6>
      </RXR.1>
    </RXR>
  </VXU_V04.ORDER>
  <VXU_V04.ORDER>
    <ORC>
      <ORC.1>RE</ORC.1>
      <ORC.3>
        <EI.1>197028</EI.1>
        <EI.2>DCS</EI.2>
      </ORC.3>
      <ORC.10>
        <XCN.2>
          <FN.1>Clerk</FN.1>
        </XCN.2>
        <XCN.3>Myron</XCN.3>
      </ORC.10>
      <ORC.12>
        <XCN.2>
          <FN.1>Pediatric</FN.1>
        </XCN.2>
        <XCN.3>MARY</XCN.3>
        <XCN.10>L</XCN.10>
        <XCN.21>MD</XCN.21>
      </ORC.12>
    </ORC>
    <RXA>
      <RXA.1>0</RXA.1>
      <RXA.2>1</RXA.2>
      <RXA.3>
        <TS.1>20090531132511</TS.1>
      </RXA.3>
      <RXA.4>
        <TS.1>20090531132511</TS.1>
      </RXA.4>
      <RXA.5>
        <CE.1>110</CE.1>
        <CE.2>DTAP-Hep B-IPV</CE.2>
        <CE.3>CVX</CE.3>
      </RXA.5>
      <RXA.6>999</RXA.6>
      <RXA.9>
        <CE.1>00</CE.1>
        <CE.2>new immunization record</CE.2>
        <CE.3>NIP0001</CE.3>
      </RXA.9>
      <RXA.10>
        <XCN.2>
          <FN.1>Sticker</FN.1>
        </XCN.2>
        <XCN.3>Nurse</XCN.3>
      </RXA.10>
      <RXA.11>
        <LA2.4>
          <HD.1>DCS_DC</HD.1>
        </LA2.4>
      </RXA.11>
      <RXA.15>xy3939</RXA.15>
      <RXA.17>
        <CE.1>SKB</CE.1>
        <CE.2>GSK</CE.2>
        <CE.3>MVX</CE.3>
      </RXA.17>
    </RXA>
    <RXR>
      <RXR.1>
        <CE.1>IM</CE.1>
        <CE.2>IM</CE.2>
        <CE.3>HL70162</CE.3>
        <CE.4>C28161</CE.4>
        <CE.5>IM</CE.5>
        <CE.6>NCIT</CE.6>
      </RXR.1>
    </RXR>
  </VXU_V04.ORDER>

非常感谢任何建议。

谢谢!

1 个答案:

答案 0 :(得分:0)

当您查找EI.x字段时,您尚未处于正确的级别:

<xsl:for-each select="a:ORC">
 <xsl:choose>
     <xsl:when test="count(a:ORC.3)= 0">
        <dil:message type="error">ORC-3 Filler Order Number not found but is required.</dil:message>
     </xsl:when> 
     <xsl:when test="count(a:ORC.3)> 1">
         <dil:message type="error">Only one Filler Order Number (ORC-3) allowed per Order Segment.</dil:message>
     </xsl:when> 
     <xsl:when test="count(a:EI.1)> 1">
         <dil:message type="error">When present, only one Filler Order Number (ORC-3) allowed per Order Segment.</dil:message>
     </xsl:when> 
     <xsl:when test="count(a:EI.1)= 0">
         <dil:message type="error">When Filler Order Number Present, Entity Identifier (ORC-3/EI-1) is required.</dil:message>
     </xsl:when>
      <xsl:when test="count(a:EI.3)= 0 and count(a:EI.2)= 0">
         <dil:message type="error">When Filler Order Number Present, Namespace ID (ORC-3/EI-2) or Universal ID (ORC-3/EI-3) is required.</dil:message>
     </xsl:when>  
     <xsl:when test="count(a:EI.3)= 1 and count(a:EI.4)= 0">
       <dil:message type="error">When Filler Order Number Universal ID is present, Universal ID Type (ORC-3/EI-4) is required.</dil:message>
     </xsl:when>
     <xsl:when test="count(a:EI.3)= 0 and count(a:EI.4)= 0">
      <dil:message type="error">When Filler Order Number Universal ID is not present, Universal ID Type (ORC-3/EI-4) is not supported.</dil:message>
     </xsl:when> 
     <xsl:when test="count(a:ORC.3)= 1">
         <dil:message type="info">Value of '<xsl:value-of select="./a:EI.1"/>', '<xsl:value-of select="./a:EI.2"/>', '<xsl:value-of select="./a:EI.3"/>', '<xsl:value-of select="./a:EI.4"/>' found for Filler Order Number (ORC-2).</dil:message>
     </xsl:when>
 </xsl:choose>
</xsl:for-each>  

你需要添加另一个级别才能进入EI。您可以使用额外的for-each / select,或者通过更改xpath来执行此操作。

<xsl:for-each select="a:ORC">
 <xsl:choose>
     <xsl:when test="count(a:ORC.3)= 0">
        <dil:message type="error">ORC-3 Filler Order Number not found but is required.</dil:message>
     </xsl:when> 
     <xsl:when test="count(a:ORC.3)> 1">
         <dil:message type="error">Only one Filler Order Number (ORC-3) allowed per Order Segment.</dil:message>
     </xsl:when> 
     <xsl:when test="count(a:ORC.3/a:EI.1)> 1">
         <dil:message type="error">When present, only one Filler Order Number (ORC-3) allowed per Order Segment.</dil:message>
     </xsl:when> 
     <xsl:when test="count(a:ORC.3/a:EI.1)= 0">
         <dil:message type="error">When Filler Order Number Present, Entity Identifier (ORC-3/EI-1) is required.</dil:message>
     </xsl:when>
      <xsl:when test="count(a:ORC.3/a:EI.3)= 0 and count(a:ORC.3/a:EI.2)= 0">
         <dil:message type="error">When Filler Order Number Present, Namespace ID (ORC-3/EI-2) or Universal ID (ORC-3/EI-3) is required.</dil:message>
     </xsl:when>  
     <xsl:when test="count(a:ORC.3/a:EI.3)= 1 and count(a:ORC.3/a:EI.4)= 0">
       <dil:message type="error">When Filler Order Number Universal ID is present, Universal ID Type (ORC-3/EI-4) is required.</dil:message>
     </xsl:when>
     <xsl:when test="count(a:ORC.3/a:EI.3)= 0 and count(a:ORC.3/a:EI.4)= 0">
      <dil:message type="error">When Filler Order Number Universal ID is not present, Universal ID Type (ORC-3/EI-4) is not supported.</dil:message>
     </xsl:when> 
     <xsl:when test="count(a:ORC.3/a:ORC.3)= 1">
         <dil:message type="info">Value of '<xsl:value-of select="./a:EI.1"/>', '<xsl:value-of select="./a:EI.2"/>', '<xsl:value-of select="./a:EI.3"/>', '<xsl:value-of select="./a:EI.4"/>' found for Filler Order Number (ORC-2).</dil:message>
     </xsl:when>
 </xsl:choose>
</xsl:for-each>