XSLT验证:使用for-each

时间:2014-01-30 17:48:54

标签: xml validation xslt

首先,我将首先说我不是程序员。然而,不知何故,我一直背负着编写XSLT验证模板的困境。我正在尽我所能,尽我所能,并且能够从搜索网络中获得。但是,我使用'for-each'结构坚持使用一条逻辑。无论如何,如果你很友好地回答,请慢慢说话并用小词!

我正在尝试编写一个分析XML消息的XSLT验证规则。我要验证的消息部分的基本结构是

<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.1> Required; only one ORC.1 field per ORC Segment allowed

在我的示例邮件中,我有三个<VXU_V04.ORDER>组。我在下面写了验证模板。当我使用三个订单组对我的消息测试我的验证模板时,我没有得到任何输出。我在ORC Segment层面上有另一个版本的模板和另一个'for-each'检查,但后来我得到了输出“每个订单组只允许一个订单段(ORC)。”三次(大概是每个订单组)。测试消息是正确的,因为每个订单组只有一个ORC段,所以显然我的逻辑是错误的。 (某些ORC.1值不正确,所以我希望在检查中得到一条消息,ORC.1的值不是'RE'。)

这是我的验证模板代码:

<?xml version="1.0" encoding="UTF-8"?>
<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 'RE'
         -->
    <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="ORC1Field" select="/a:VXU_V04/a:VXU_V04.ORDER/a:ORC/a:ORC.1"/>
    <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:otherwise>
                          <xsl:for-each select="./a:VXU_V04.ORDER">
                             <xsl:choose>
                                 <xsl:when test="count(./a:ORC)> 0">  
                        <dil:message type="error">Only one Order Segment (ORC) allowed per Order Group.</dil:message>
                                 </xsl:when>
                                 <xsl:when test="count(./a:ORC.1)= 0">
                                <dil:message type="error">ORC-1 Order Control not found but is required.</dil:message>
                                 </xsl:when> 
                                 <xsl:when test="count(./a:ORC.1)> 1">
                                <dil:message type="error">Only one Order Control (ORC-1) allowed per Order Segment.</dil:message>
                                 </xsl:when> 
                                 <xsl:when test="./a:ORC.1!='RE'">
                                <dil:message type="error">ORC-1 Order Control must be equal to 'RE'.  Found '<xsl:value-of select="./a:ORC.1"/>' in message.</dil:message>
                                 </xsl:when>                      
                                 <xsl:when test="./a:ORC.1='RE'">
                                     <dil:message type="info">Valid value of '<xsl:value-of select="./a:ORC.1"/>' found for Order Control (ORC-1).</dil:message>
                                 </xsl:when>
                             </xsl:choose>
                          </xsl:for-each>  
                       </xsl:otherwise>
                      </xsl:choose> 
             </xsl:for-each>      
        </dil:filterruleresult>
    </xsl:template>
</xsl:stylesheet>

以下是示例消息:

  <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 :(得分:1)

也许你应该考虑成为一名程序员,因为你的XSLT对非程序员来说并不是坏事。

无论如何,要尝试解决此问题,您的订单组都有 xsl:for-each 循环

<xsl:for-each select="$OrderGroup">

这意味着您按预期循环 VXU_V04.ORDER 元素。但是在这个循环中,在 xsl:when 条件之一中,你可以这样做

<xsl:for-each select="./a:VXU_V04.ORDER">

但这意味着它正在寻找当前 VXU_V04.ORDER 元素的名为 VXU_V04.ORDER 的子元素。我不认为这是你想要的。您已经在遍历这些元素,因此您不需要继续循环。

也许您的意思是寻找孩子 ORC 元素

<xsl:for-each select="a:ORC">

(你不需要在这里用./作为前缀,因为这是默认选择器。

这将是当前<xsl:when test="count(./a:ORC)> 0">测试需要移出内循环并进入外循环,并被更改为测试大于1,但其余条件应该有效。

试试这个XSLT

<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 'RE'
         -->
    <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="ORC1Field" select="/a:VXU_V04/a:VXU_V04.ORDER/a:ORC/a:ORC.1"/>
    <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.1)= 0">
                                <dil:message type="error">ORC-1 Order Control not found but is required.</dil:message>
                                 </xsl:when> 
                                 <xsl:when test="count(a:ORC.1)> 1">
                                <dil:message type="error">Only one Order Control (ORC-1) allowed per Order Segment.</dil:message>
                                 </xsl:when> 
                                 <xsl:when test="a:ORC.1!='RE'">
                                <dil:message type="error">ORC-1 Order Control must be equal to 'RE'.  Found '<xsl:value-of select="./a:ORC.1"/>' in message.</dil:message>
                                 </xsl:when>                      
                                 <xsl:when test="a:ORC.1='RE'">
                                     <dil:message type="info">Valid value of '<xsl:value-of select="./a:ORC.1"/>' found for Order Control (ORC-1).</dil:message>
                                 </xsl:when>
                             </xsl:choose>
                          </xsl:for-each>  
                       </xsl:otherwise>
                      </xsl:choose> 
             </xsl:for-each>      
        </dil:filterruleresult>
    </xsl:template>
</xsl:stylesheet>

当然,<xsl:for-each select="a:ORC">有点矫枉过正,因为你已经验证过只有一个。理想情况下,您可以在此处使用 xsl:apply-templates 。也试试这个XSLT

<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 'RE'
         -->
    <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="ORC1Field" select="/a:VXU_V04/a:VXU_V04.ORDER/a:ORC/a:ORC.1"/>
    <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:apply-templates select="a:ORC" /> 
                       </xsl:otherwise>
                      </xsl:choose> 
             </xsl:for-each>      
        </dil:filterruleresult>
    </xsl:template>

    <xsl:template match="a:ORC">
           <xsl:choose>
                <xsl:when test="count(./a:ORC.1)= 0">
              <dil:message type="error">ORC-1 Order Control not found but is required.</dil:message>
                </xsl:when> 
                <xsl:when test="count(./a:ORC.1)> 1">
              <dil:message type="error">Only one Order Control (ORC-1) allowed per Order Segment.</dil:message>
                </xsl:when> 
                <xsl:when test="./a:ORC.1!='RE'">
              <dil:message type="error">ORC-1 Order Control must be equal to 'RE'.  Found '<xsl:value-of select="./a:ORC.1"/>' in message.</dil:message>
                </xsl:when>                      
                <xsl:when test="./a:ORC.1='RE'">
                    <dil:message type="info">Valid value of '<xsl:value-of select="./a:ORC.1"/>' found for Order Control (ORC-1).</dil:message>
                </xsl:when>
            </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

如果没有别的,这就是陈述的嵌套。