【问题标题】:Delete Duplicate Elements from Xml and should not delete null values inXSLT从 Xml 中删除重复元素,并且不应删除 XSLT 中的空值
【发布时间】:2020-06-06 16:24:18
【问题描述】:

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

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

输入 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-ControlName />      
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>4</Step-ID>     
              <PlayBack--ControlInfo-ControlName />     
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>5</Step-ID>      
              <PlayBack--ControlInfo-ControlName>Edit</PlayBack--ControlInfo-ControlName>     
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>6</Step-ID>      
              <PlayBack--ControlInfo-ControlName>Replace...\tCtrl+H</PlayBack--ControlInfo-ControlName>     
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>12</Step-ID>     
              <PlayBack--ControlInfo-ControlName />     
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>13</Step-ID>     
              <PlayBack--ControlInfo-ControlName>Edit</PlayBack--ControlInfo-ControlName>      
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>14</Step-ID>      
              <PlayBack--ControlInfo-ControlName>Replace...\tCtrl+H</PlayBack--ControlInfo-ControlName>      
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>15</Step-ID>     
              <PlayBack--ControlInfo-ControlName>Cancel</PlayBack--ControlInfo-ControlName>
            </Step>
          </GStep>
          <GStep DialogName="Replace">
             <Step DialogName="Replace">
                <Step-ID>8</Step-ID>     
                <PlayBack--ControlInfo-ControlName />      
             </Step>
             <Step DialogName="Replace">
                <Step-ID>9</Step-ID>      
                <PlayBack--ControlInfo-ControlName>Cancel</PlayBack--ControlInfo-ControlName>     
             </Step>
             <Step DialogName="Replace">
                <Step-ID>10</Step-ID>      
                <PlayBack--ControlInfo-ControlName />     
             </Step>
             <Step DialogName="Replace">
                <Step-ID>16</Step-ID>     
                <PlayBack--ControlInfo-ControlName />     
             </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-ControlName />      
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>4</Step-ID>     
              <PlayBack--ControlInfo-ControlName />     
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>5</Step-ID>      
              <PlayBack--ControlInfo-ControlName>Edit</PlayBack--ControlInfo-ControlName>     
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>6</Step-ID>      
              <PlayBack--ControlInfo-ControlName>Replace...\tCtrl+H</PlayBack--ControlInfo-ControlName>     
            </Step>
            <Step DialogName="Untitled - Notepad">
              <Step-ID>12</Step-ID>     
              <PlayBack--ControlInfo-ControlName />     
            </Step>
             <Step DialogName="Untitled - Notepad">
              <Step-ID>15</Step-ID>     
              <PlayBack--ControlInfo-ControlName>Cancel</PlayBack--ControlInfo-ControlName>
            </Step>
          </GStep>
          <GStep DialogName="Replace">
             <Step DialogName="Replace">
                <Step-ID>8</Step-ID>     
                <PlayBack--ControlInfo-ControlName />      
             </Step>
             <Step DialogName="Replace">
                <Step-ID>9</Step-ID>      
                <PlayBack--ControlInfo-ControlName>Cancel</PlayBack--ControlInfo-ControlName>     
             </Step>
             <Step DialogName="Replace">
                <Step-ID>10</Step-ID>      
                <PlayBack--ControlInfo-ControlName />     
             </Step>
             <Step DialogName="Replace">
                <Step-ID>16</Step-ID>     
                <PlayBack--ControlInfo-ControlName />     
             </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="ControlNameInfo" match="Step" use="PlayBack--ControlInfo-ControlName"/>
     <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('ControlNameInfo', PlayBack-- 
        ControlInfo-ControlName)[1]))]"/>
        </xsl:stylesheet>

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

非常感谢。

【问题讨论】:

    标签: xslt xslt-1.0 xslt-2.0 xslt-grouping


    【解决方案1】:

    假设它是 XSLT 2 或 3,并且您想使用 key 函数,您似乎需要添加一个谓词来仅检查非空 Steps 并且您还需要使用 key 的第三个参数限制搜索到祖先GStep的函数:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        exclude-result-prefixes="#all"
        version="3.0">
    
      <xsl:strip-space elements="*"/>
      <xsl:output indent="yes"/>
    
      <xsl:mode on-no-match="shallow-copy"/>
    
      <xsl:key name="dups" match="Step[normalize-space(PlayBack--ControlInfo-ControlName)]" use="PlayBack--ControlInfo-ControlName"/>
    
      <xsl:template match="Step[normalize-space(PlayBack--ControlInfo-ControlName)][not(. is key('dups', PlayBack--ControlInfo-ControlName, ancestor::GStep)[1])]"/>
    
    </xsl:stylesheet>
    

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

    【讨论】:

    • 您好,感谢您的回复。如果我想在 1.0 版中做。它在 Visual Studion 2.0 和 3.0 版本中给出异常如下 studioXslTransformException --------------------- 预期令牌')',发现'是'。
    • 那就不要标记为xslt-2
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    相关资源
    最近更新 更多