使用XSLT删除XML消息中的Empty Elements字段和Default值

时间:2012-01-02 17:48:16

标签: xml xslt

需求的详细规范是,在Request消息中有一个名称为IgnoreEmptyElementInd的字段,它是boolean类型。如果该字段为True,则需要忽略请求消息中的空元素字段,并将默认值(如“-111”shd)设置为找到的默认值。如果为false,则只有在找到默认值时才需要删除空元素,否则将通知它。

我的输入消息是

<SampleUpdRq>
     <RqUID>00000000-0000-0000-0000-000000000000</RqUID>
     <UpdMsgRqHdr>
          <ContextRqHdr>
              <RsSelURL>111</RsSelURL>
              <NetworkTrnData>
                    <TerminalIdent>a</TerminalIdent>
                    <Name>111</Name>
              </NetworkTrnData>
              <ClientApp>
                     <Org>dweer</Org>
                     <Name>aaaaaaaaaaaaaaaaaaa</Name>
                     <Version>112</Version>
                     <Channel>abc</Channel>
              </ClientApp>
          </ContextRqHdr>
          <IgnoreEmptyElementInd>true</IgnoreEmptyElementInd>
     </UpdMsgRqHdr>
     <SampleKeys>
           <SampleId>aaaaaaaaaaaaaaaaaaa</SampleId>
           <AltSampleIdentifiers>
              <SampleIdent>
                   <SampleIdentType>-111</SampleIdentType>
                    <SampleIdentValue>ttttttt</SampleIdentValue>
              </SampleIdent>
              <SampleIdentType/>
           </AltSampleIdentifiers>
           <SampleType>
                <SampleTypeValue>MMA</SampleTypeValue>
                <SampleTypeSrc>bbc</SampleTypeSrc>
                <CommercialSampleType></CommercialSampleType>
           </SampleType>
           <CommercialSampleType>-111</CommercialSampleType>
           <COID>aaaaa</COID>
       </SampleKeys>
</SampleUpdRq> 

如果IgnoreEmptyElementInd的值为 true ,则输出shd就像

<SampleUpdRq>
     <RqUID>00000000-0000-0000-0000-000000000000</RqUID>
     <UpdMsgRqHdr>
          <ContextRqHdr>
              <RsSelURL>111</RsSelURL>
              <NetworkTrnData>
                    <TerminalIdent>a</TerminalIdent>
                    <Name>111</Name>
              </NetworkTrnData>
              <ClientApp>
                     <Org>dweer</Org>
                     <Name>aaaaaaaaaaaaaaaaaaa</Name>
                     <Version>112</Version>
                     <Channel>abc</Channel>
              </ClientApp>
          </ContextRqHdr>
          <IgnoreEmptyElementInd>true</IgnoreEmptyElementInd>
     </UpdMsgRqHdr>
     <SampleKeys>
           <SampleId>aaaaaaaaaaaaaaaaaaa</SampleId>
           <AltSampleIdentifiers>
              <SampleIdent>
                   <SampleIdentType>Default Found</SampleIdentType>
                    <SampleIdentValue>ttttttt</SampleIdentValue>
              </SampleIdent>
           </AltSampleIdentifiers>
           <SampleType>
                <SampleTypeValue>MMA</SampleTypeValue>
                <SampleTypeSrc>bbc</SampleTypeSrc>
           </SampleType>
           <CommercialSampleType>Default Found</CommercialSampleType>
           <COID>aaaaa</COID>
       </SampleKeys>
</SampleUpdRq>

如果IgnoreEmptyElementInd的值为 false ,则输出shd就像

<SampleUpdRq>
     <RqUID>00000000-0000-0000-0000-000000000000</RqUID>
     <UpdMsgRqHdr>
          <ContextRqHdr>
              <RsSelURL>111</RsSelURL>
              <NetworkTrnData>
                    <TerminalIdent>a</TerminalIdent>
                    <Name>111</Name>
              </NetworkTrnData>
              <ClientApp>
                     <Org>dweer</Org>
                     <Name>aaaaaaaaaaaaaaaaaaa</Name>
                     <Version>112</Version>
                     <Channel>abc</Channel>
              </ClientApp>
          </ContextRqHdr>
          <IgnoreEmptyElementInd>false</IgnoreEmptyElementInd>
     </UpdMsgRqHdr>
     <SampleKeys>
           <SampleId>aaaaaaaaaaaaaaaaaaa</SampleId>
           <AltSampleIdentifiers>
              <SampleIdent>
                   <SampleIdentType>Default Found</SampleIdentType>
                    <SampleIdentValue>ttttttt</SampleIdentValue>
              </SampleIdent>
              <SampleIdentType/>
           </AltSampleIdentifiers>
           <SampleType>
                <SampleTypeValue>MMA</SampleTypeValue>
                <SampleTypeSrc>bbc</SampleTypeSrc>
            <CommercialSampleType/>
           </SampleType>
           <CommercialSampleType>Default Found</CommercialSampleType>
           <COID>aaaaa</COID>
       </SampleKeys>
</SampleUpdRq>

我写过像这样的XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>     

<xsl:template match="*[not(node())]" />     

<xsl:template match="*[. = '-111']">       
  <xsl:copy>          
    <xsl:text>Default Found</xsl:text>
 </xsl:copy>    
</xsl:template>     

<xsl:template match="@*|node()">
    <xsl:copy>          
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>    
</xsl:template> 

</xsl:stylesheet> 

真的很感激专业知识的帮助和抱歉的混乱。提前谢谢。

1 个答案:

答案 0 :(得分:1)

在你的一条评论中,你说“IgnoreEmptyElementInd之后的元素,应该应用规则”,在这种情况下听起来你需要在XPath中使用'前面'轴来确定值。

因此,当 IgnoreEmptyElementInd 设置为true时,您可以使用此模板扩展XSLT以忽略空节点。

<xsl:template 
   match="*[not(node())][preceding::IgnoreEmptyElementInd[. = 'true']]"/>

因此,给出以下XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="*[not(node())][preceding::IgnoreEmptyElementInd[. = 'true']]"/>

   <xsl:template match="*[. = '-111']">
      <xsl:copy>
         <xsl:text>Default Found</xsl:text>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

当应用于您的示例XML时,输出以下内容

<SampleUpdRq>
   <RqUID>00000000-0000-0000-0000-000000000000</RqUID>
   <UpdMsgRqHdr>
      <ContextRqHdr>
         <RsSelURL>111</RsSelURL>
         <NetworkTrnData>
            <TerminalIdent>a</TerminalIdent>
            <Name>111</Name>
         </NetworkTrnData>
         <ClientApp>
            <Org>dweer</Org>
            <Name>aaaaaaaaaaaaaaaaaaa</Name>
            <Version>112</Version>
            <Channel>abc</Channel>
         </ClientApp>
      </ContextRqHdr>
      <IgnoreEmptyElementInd>true</IgnoreEmptyElementInd>
   </UpdMsgRqHdr>
   <SampleKeys>
      <SampleId>aaaaaaaaaaaaaaaaaaa</SampleId>
      <AltSampleIdentifiers>
         <SampleIdent>
            <SampleIdentType>Default Found</SampleIdentType>
            <SampleIdentValue>ttttttt</SampleIdentValue>
         </SampleIdent>
      </AltSampleIdentifiers>
      <SampleType>
         <SampleTypeValue>MMA</SampleTypeValue>
         <SampleTypeSrc>bbc</SampleTypeSrc>
      </SampleType>
      <CommercialSampleType>Default Found</CommercialSampleType>
      <COID>aaaaa</COID>
   </SampleKeys>
</SampleUpdRq>

IgnoreEmptyElementInd 设置为'false'时,输出以下内容

<SampleUpdRq>
   <RqUID>00000000-0000-0000-0000-000000000000</RqUID>
   <UpdMsgRqHdr>
      <ContextRqHdr>
         <RsSelURL>111</RsSelURL>
         <NetworkTrnData>
            <TerminalIdent>a</TerminalIdent>
            <Name>111</Name>
         </NetworkTrnData>
         <ClientApp>
            <Org>dweer</Org>
            <Name>aaaaaaaaaaaaaaaaaaa</Name>
            <Version>112</Version>
            <Channel>abc</Channel>
         </ClientApp>
      </ContextRqHdr>
      <IgnoreEmptyElementInd>false</IgnoreEmptyElementInd>
   </UpdMsgRqHdr>
   <SampleKeys>
      <SampleId>aaaaaaaaaaaaaaaaaaa</SampleId>
      <AltSampleIdentifiers>
         <SampleIdent>
            <SampleIdentType>Default Found</SampleIdentType>
            <SampleIdentValue>ttttttt</SampleIdentValue>
         </SampleIdent>
         <SampleIdentType/>
      </AltSampleIdentifiers>
      <SampleType>
         <SampleTypeValue>MMA</SampleTypeValue>
         <SampleTypeSrc>bbc</SampleTypeSrc>
         <CommercialSampleType/>
      </SampleType>
      <CommercialSampleType>Default Found</CommercialSampleType>
      <COID>aaaaa</COID>
   </SampleKeys>
</SampleUpdRq>