【问题标题】:remove the Duplicate Elements from XML file and should not delete null values in XSLT file从 XML 文件中删除重复元素,并且不应删除 XSLT 文件中的空值
【发布时间】:2020-06-07 10:28:43
【问题描述】:

如果有人可以帮助我创建 xslt 以根据重复元素的值(PlayBack--ControlInfo-ControlName1)而不是空值从 XML 中删除重复节点,我将不胜感激。

我想从 GStep/Step 中删除除空值(PlayBack--ControlInfo-ControlName1)之外的所有重复元素

输入 XML

<?xml version="1.0" encoding="utf-8"?>
      <Document>
       <Meta>
         <GpsFile>notepad_may_30_file</GpsFile>
         <GpsId>36fa4fe8-9691-4a7f-8bc1-9543f6b7d29a</GpsId>
          <ExePath>
             <ExePath1>C:\WINDOWS\SYSTEM32\notepad.exe</ExePath1>
          </ExePath>
        </Meta>
         <Process>
            <GStep DialogName="Untitled - Notepad">
             <Step DialogName="Untitled - Notepad">
              <Step-ID>3</Step-ID>     
              <PlayBack--ControlInfo-ControlName1 />      
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>4</Step-ID>     
              <PlayBack--ControlInfo-ControlName1 />     
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>5</Step-ID>      
              <PlayBack--ControlInfo-ControlName1>Edit</PlayBack--ControlInfo-ControlName1>     
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>6</Step-ID>      
              <PlayBack--ControlInfo-ControlName1>Replace...\tCtrl+H</PlayBack--ControlInfo-ControlName1>     
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>12</Step-ID>     
              <PlayBack--ControlInfo-ControlName1 />     
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>13</Step-ID>     
              <PlayBack--ControlInfo-ControlName1>Edit</PlayBack--ControlInfo-ControlName1>      
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>14</Step-ID>      
              <PlayBack--ControlInfo-ControlName1>Replace...\tCtrl+H</PlayBack--ControlInfo-ControlName1>      
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>15</Step-ID>     
              <PlayBack--ControlInfo-ControlName1>Cancel</PlayBack--ControlInfo-ControlName1>
            </Step>
          </GStep>
          <GStep DialogName="Replace">
             <Step DialogName="Replace">
                <Step-ID>8</Step-ID>     
                <PlayBack--ControlInfo-ControlName1 />      
             </Step>
             <Step DialogName="Replace">
                <Step-ID>9</Step-ID>      
                <PlayBack--ControlInfo-ControlName1>Cancel</PlayBack--ControlInfo-ControlName1>     
             </Step>
             <Step DialogName="Replace">
                <Step-ID>10</Step-ID>      
                <PlayBack--ControlInfo-ControlName1 />     
             </Step>
             <Step DialogName="Replace">
                <Step-ID>16</Step-ID>     
                <PlayBack--ControlInfo-ControlName1 />     
             </Step>
           </GStep>
         </Process>
       </Document>

实际上期待如下结果。

<?xml version="1.0" encoding="utf-8"?>
      <Document>
       <Meta>
         <GpsFile>notepad_may_30_file</GpsFile>
         <GpsId>36fa4fe8-9691-4a7f-8bc1-9543f6b7d29a</GpsId>
          <ExePath>
             <ExePath1>C:\WINDOWS\SYSTEM32\notepad.exe</ExePath1>
          </ExePath>
        </Meta>
         <Process>
            <GStep DialogName="Untitled - Notepad">
             <Step DialogName="Untitled - Notepad">
              <Step-ID>3</Step-ID>     
              <PlayBack--ControlInfo-ControlName1 />      
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>4</Step-ID>     
              <PlayBack--ControlInfo-ControlName1 />     
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>5</Step-ID>      
              <PlayBack--ControlInfo-ControlName1>Edit</PlayBack--ControlInfo-ControlName1>     
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>6</Step-ID>      
              <PlayBack--ControlInfo-ControlName1>Replace...\tCtrl+H</PlayBack--ControlInfo-ControlName1>     
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>12</Step-ID>     
              <PlayBack--ControlInfo-ControlName1 />     
            </Step>
             <Step DialogName="Untitled - Notepad">
              <Step-ID>15</Step-ID>     
              <PlayBack--ControlInfo-ControlName1>Cancel</PlayBack--ControlInfo-ControlName1>
            </Step>
          </GStep>
          <GStep DialogName="Replace">
             <Step DialogName="Replace">
                <Step-ID>8</Step-ID>     
                <PlayBack--ControlInfo-ControlName1 />      
             </Step>
             <Step DialogName="Replace">
                <Step-ID>9</Step-ID>      
                <PlayBack--ControlInfo-ControlName1>Cancel</PlayBack--ControlInfo-ControlName1>     
             </Step>
             <Step DialogName="Replace">
                <Step-ID>10</Step-ID>      
                <PlayBack--ControlInfo-ControlName1 />     
             </Step>
             <Step DialogName="Replace">
                <Step-ID>16</Step-ID>     
                <PlayBack--ControlInfo-ControlName1 />     
             </Step>
           </GStep>
         </Process>
       </Document>

Xslt 代码 sn-p.

<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:key name="ControlNameInfo1" match="Step" use="PlayBack--ControlInfo-ControlName1"/>
     <xsl:template match="node()|@*">
      <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>
   <xsl:template match="GStep/Step[not(generate-id() = generate-id(key('ControlNameInfo1', PlayBack-- 
        ControlInfo-ControlName)[1]))]"/>
        </xsl:stylesheet>

应该删除重复的值但不应该删除空的PlayBack--ControlInfo-ControlName1

非常感谢。

【问题讨论】:

    标签: xslt xslt-1.0 xslt-grouping


    【解决方案1】:

    我在您之前的问题中发布的 XSLT 3 方法的 XSLT 1.0 转录将是

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="1.0">
    
      <xsl:strip-space elements="*"/>
      <xsl:output indent="yes"/>
    
      <xsl:template match="@* | node()">
          <xsl:copy>
              <xsl:apply-templates select="@* | node()"/>
          </xsl:copy>
      </xsl:template>
    
      <xsl:key name="dups" match="Step[normalize-space(PlayBack--ControlInfo-ControlName)]" use="concat(generate-id(..), '|', PlayBack--ControlInfo-ControlName)"/>
    
      <xsl:template match="Step[normalize-space(PlayBack--ControlInfo-ControlName)][not(generate-id() = generate-id(key('dups', concat(generate-id(..), '|', PlayBack--ControlInfo-ControlName))[1]))]"/>
    
    </xsl:stylesheet>
    

    https://xsltfiddle.liberty-development.net/bEzkTcp/5

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-12
      • 1970-01-01
      • 2018-04-04
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      相关资源
      最近更新 更多