如何基于多个字段过滤xml

时间:2019-08-17 16:30:51

标签: xslt

我只需要获取end_date = 9999-12-31并且如果操作不等于“删除”,就需要过滤job_information记录的帮助

输入XML如下

queryCompoundEmployeeResponse  
CompoundEmployee  
Person  
fields...fields  
fields...fields  
fields...fields  
Employment_Information  
fields...fields  
fields...fields  
      job_information
      Action..Action
      date...date  
      job_information
      Action...Action
      date...date 

我被困住了

`<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
  <xsl:template match="@* | node()">
   <xsl:copy>
   <xsl:apply-templates select="@* | node()"/>
   </xsl:copy>
   </xsl:template>
   <xsl:template match="employment_information">
  <xsl:copy>
    <xsl:apply-templates select="*[not(self::job_information)]"/>
   <xsl:copy-of select="employment_information[job_information/end_date = 
  '9999-12-31']"/>
</xsl:copy>

以下是输入的XML

      <?xml version="1.0" encoding="UTF-8"?>
      <queryCompoundEmployeeResponse>
      <CompoundEmployee>
      <id></id>
      <person>
      <employment_information>
      <action>NO CHANGE</action>
      <assignment_class></assignment_class>
      <job_information>
      <action>NO CHANGE</action>
      <end_date>9999-12-31</end_date>
      <event>5</event>
      </job_information>
      <job_information>
      <action>DELETE</action>
      <end_date>1998-12-31</end_date>
      <event>5</event>
      </job_information>
      </employment_information>
      </person>
      <execution_timestamp>2018-09-18T12:06:05.000Z</execution_timestamp>
      <version_id></version_id>
     </CompoundEmployee>
    </queryCompoundEmployeeResponse>

输出应为以下XML。应该删除第二个job_information

      <?xml version="1.0" encoding="UTF-8"?>
      <queryCompoundEmployeeResponse>
      <CompoundEmployee>
      <id></id>
      <person>
      <employment_information>
      <action>NO CHANGE</action>
      <assignment_class></assignment_class>
      <job_information>
      <action>NO CHANGE</action>
      <end_date>9999-12-31</end_date>
      <event>5</event>
      </job_information>
      </employment_information>
      </person>
      <execution_timestamp>2018-09-18T12:06:05.000Z</execution_timestamp>
      <version_id></version_id>
     </CompoundEmployee>
    </queryCompoundEmployeeResponse>

1 个答案:

答案 0 :(得分:0)

  

仅通过获取end_date = 9999-12-31并且如果操作不等于“ DELETE”来过滤job_information记录

我认为可以将其翻译为:

XSLT 1.0

https://console.cloud.google.com/m/marketplace/procurement/jwt?purpose=SIGNUP...

删除您不需要的<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <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="job_information[not(end_date = '9999-12-31' and action != 'DELETE')]"/> </xsl:stylesheet> 元素。要按照开始的方式进行操作-即保持所需的job_information元素,则必须为:

job_information

请注意,这可能会更改元素的原始顺序,因为所需的<xsl:template match="employment_information"> <xsl:copy> <xsl:apply-templates select="*[not(self::job_information)]"/> <xsl:copy-of select="job_information[end_date = '9999-12-31' and action != 'DELETE']"/> </xsl:copy> </xsl:template> 元素将始终位于末尾。