xslt:根据子元素值

时间:2016-08-27 21:23:30

标签: xml xslt

我需要你帮助修复我的xslt。

我的要求是根据其死亡(孙子)值删除特定的子节点。

在下面的示例中,我需要编写一个xslt,它给出了包含xml的内容  其死亡“SID”值不等于“1113181”

输入XML:

<CDM CDMVersion="1.3" FType="Transactional" FOID="85b40b86-e6f1-4813-a5f4-0b98c90bd72d" CreationDateTime="2016-08-19T19:04:25" xmlns="http://www.example.com/ns/CDM/v1.3" xmlns:xyz="http://www.example.com/ns/CDM/MData">
   <TestData EduOID="LX4211-204" MDataVersionOID="34" xyz:AuditSubCategoryName="Verify">
      <SubData SubKey="5117f771-790f-4cde-b261-1a2db0dcd04b" xyz:SubKeyType="SubUUID" xyz:SubName="204-0114-002">
         <SRef LocOID="LX4211204_0114"/>
         <SubEventData EduEventOID="EOT" EduEventRepeatKey="EOT[1]" xyz:InstanceId="76194">
            <FmData FmOID="VS2" FmRepeatKey="1" xyz:DataPageId="388465">
               <IGroupData IGroupOID="VS2" xyz:RecordId="1044557">
                  <IData ItemOID="VS2.VSORRES4_2" TransactionType="Upsert" xyz:Verify="Yes">
                     <Record>
                        <TesterRef TesterOID="bballardamad1"/>
                        <LocRef LocOID="LX4211204_0114"/>
                        <DateTimeStamp>2016-08-16T17:07:15</DateTimeStamp>
                        <ReasonForChange/>
                        <SID>1113181</SID>
                     </Record>
                  </IData>
               </IGroupData>
            </FmData>
         </SubEventData>
      </SubData>
   </TestData>
   <TestData EduOID="LX4211-204" MDataVersionOID="34" xyz:AuditSubCategoryName="QueryClose">
      <SubData SubKey="5117f771-790f-4cde-b261-1a2db0dcd04b" xyz:SubKeyType="SubUUID" xyz:SubName="204-0114-002">
         <SRef LocOID="LX4211204_0114"/>
         <SubEventData EduEventOID="EOT" EduEventRepeatKey="EOT[1]" xyz:InstanceId="76194">
            <FmData FmOID="VS2" FmRepeatKey="1" xyz:DataPageId="388465">
               <IGroupData IGroupOID="VS2" xyz:RecordId="1044557">
                  <IData ItemOID="VS2.VSORRES4_2" TransactionType="Upsert">
                     <Record>
                        <TesterRef TesterOID="bballardamad1"/>
                        <LocRef LocOID="LX4211204_0114"/>
                        <DateTimeStamp>2016-08-16T17:07:15</DateTimeStamp>
                        <ReasonForChange/>
                        <SID>1113182</SID>
                     </Record>
                     <xyz:Query QueryRepeatKey="552326" Status="Closed" Recipient="Site from CRA"/>
                  </IData>
               </IGroupData>
            </FmData>
         </SubEventData>
      </SubData>
   </TestData>
   <TestData EduOID="LX4211-204" MDataVersionOID="34" xyz:AuditSubCategoryName="Verify">
      <SubData SubKey="5117f771-790f-4cde-b261-1a2db0dcd04b" xyz:SubKeyType="SubUUID" xyz:SubName="204-0114-002">
         <SRef LocOID="LX4211204_0114"/>
         <SubEventData EduEventOID="WK1_POST" EduEventRepeatKey="WK1_POST[1]" xyz:InstanceId="119171">
            <FmData FmOID="VS2" FmRepeatKey="1" xyz:DataPageId="670872">
               <IGroupData IGroupOID="VS2" xyz:RecordId="1934354">
                  <IData ItemOID="VS2.VSORRES12" TransactionType="Upsert" xyz:Verify="Yes">
                     <Record>
                        <TesterRef TesterOID="bballardamad1"/>
                        <LocRef LocOID="LX4211204_0114"/>
                        <DateTimeStamp>2016-08-16T17:09:10</DateTimeStamp>
                        <ReasonForChange/>
                        <SID>1114610</SID>
                     </Record>
                  </IData>
               </IGroupData>
            </FmData>
         </SubEventData>
      </SubData>
   </TestData>
</CDM>

=============================================== =================================

我的XSLT:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
  </xsl:template>

<!--<xsl:template match="//SID[text()!=1113181]"/>-->
 <xsl:template match="/CDM/TestData/SubData/SubEventData/FmData/IGroupData/IData/Record/SID[text()!=68943181]"/>
</xsl:stylesheet>

预期产出:

<CDM CDMVersion="1.3" FType="Transactional" FOID="85b40b86-e6f1-4813-a5f4-0b98c90bd72d" CreationDateTime="2016-08-19T19:04:25" xmlns="http://www.example.com/ns/CDM/v1.3" xmlns:xyz="http://www.example.com/ns/CDM/MData">
   <TestData EduOID="LX4211-204" MDataVersionOID="34" xyz:AuditSubCategoryName="QueryClose">
      <SubData SubKey="5117f771-790f-4cde-b261-1a2db0dcd04b" xyz:SubKeyType="SubUUID" xyz:SubName="204-0114-002">
         <SRef LocOID="LX4211204_0114"/>
         <SubEventData EduEventOID="EOT" EduEventRepeatKey="EOT[1]" xyz:InstanceId="76194">
            <FmData FmOID="VS2" FmRepeatKey="1" xyz:DataPageId="388465">
               <IGroupData IGroupOID="VS2" xyz:RecordId="1044557">
                  <IData ItemOID="VS2.VSORRES4_2" TransactionType="Upsert">
                     <Record>
                        <TesterRef TesterOID="bballardamad1"/>
                        <LocRef LocOID="LX4211204_0114"/>
                        <DateTimeStamp>2016-08-16T17:07:15</DateTimeStamp>
                        <ReasonForChange/>
                        <SID>1113182</SID>
                     </Record>
                     <xyz:Query QueryRepeatKey="552326" Status="Closed" Recipient="Site from CRA"/>
                  </IData>
               </IGroupData>
            </FmData>
         </SubEventData>
      </SubData>
   </TestData>
   <TestData EduOID="LX4211-204" MDataVersionOID="34" xyz:AuditSubCategoryName="Verify">
      <SubData SubKey="5117f771-790f-4cde-b261-1a2db0dcd04b" xyz:SubKeyType="SubUUID" xyz:SubName="204-0114-002">
         <SRef LocOID="LX4211204_0114"/>
         <SubEventData EduEventOID="WK1_POST" EduEventRepeatKey="WK1_POST[1]" xyz:InstanceId="119171">
            <FmData FmOID="VS2" FmRepeatKey="1" xyz:DataPageId="670872">
               <IGroupData IGroupOID="VS2" xyz:RecordId="1934354">
                  <IData ItemOID="VS2.VSORRES12" TransactionType="Upsert" xyz:Verify="Yes">
                     <Record>
                        <TesterRef TesterOID="bballardamad1"/>
                        <LocRef LocOID="LX4211204_0114"/>
                        <DateTimeStamp>2016-08-16T17:09:10</DateTimeStamp>
                        <ReasonForChange/>
                        <SID>1114610</SID>
                     </Record>
                  </IData>
               </IGroupData>
            </FmData>
         </SubEventData>
      </SubData>
   </TestData>
</CDM>

1 个答案:

答案 0 :(得分:2)

您的尝试不起作用有四个原因:

  1. 如果您希望模板移除TestData,请将其与TestData匹配 - 而不是其后代;
  2. 输入XML的元素位于名称空间;你需要声明一个前缀并在匹配/选择时使用它;
  3. 如果您想保持 <TestData>,其后代SID等于1113181,那么请移除模板匹配TestData,其后代SID 等于1113181;
  4. 68943181不是1113181
  5. 尝试:

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:cdm="http://www.example.com/ns/CDM/v1.3">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="cdm:TestData[cdm:SubData/cdm:SubEventData/cdm:FmData/cdm:IGroupData/cdm:IData/cdm:Record/cdm:SID=1113181]"/>
    
    </xsl:stylesheet>