How do we filter the unnecessary knots after the knots with a certain attribute?



  • XML file:

    <a>
      <Item key="1">
        <c1>
          <d11>
          </d11>
          <d12 value="1" />
          <d13 />
        </c1>
      </Item>
    

    <b2>
    <Item key="fix">
    <d21>
    </d21>
    <d22 value="yes" />
    <d23 />
    </Item>
    </b2>

    <b3>
    <c3>
    <d31>
    </d31>
    <Item key="price">
    <e2 value="no" />
    <e3 />
    </Item>
    </c3>
    </b3>
    </a>

    File.xsl:

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

    <xsl:param name="element-name">Item</xsl:param>

    <xsl:template match="/">

    &lt;xsl:for-each select="//*[name()=$element-name]"&gt; 
    
      &lt;xsl:for-each select="ancestor-or-self::*"&gt;
    
            &lt;xsl:value-of select="name()"/&gt; 
    
            &lt;xsl:if test="position() != last()"&gt;
                &lt;xsl:text&gt;/&lt;/xsl:text&gt;
            &lt;/xsl:if&gt;
        
            &lt;xsl:if test ="name()=$element-name"&gt;
            &lt;xsl:text&gt;[@key='&lt;/xsl:text&gt;
            &lt;xsl:value-of select="@key"/&gt;
            &lt;xsl:text&gt;']/&lt;/xsl:text&gt;
            &lt;/xsl:if&gt;
        &lt;/xsl:for-each&gt;    
      
            &lt;xsl:for-each select="descendant::*"&gt;
    
            &lt;xsl:value-of select="name()"/&gt; 
    
            &lt;xsl:if test="position() != last() and not(@value)"&gt;
                &lt;xsl:text&gt;/&lt;/xsl:text&gt;
            &lt;/xsl:if&gt;
            
            &lt;xsl:if test="(.)[@value]"&gt;
                &lt;xsl:text&gt;/@value&lt;/xsl:text&gt;
            &lt;/xsl:if&gt;
            
            &lt;/xsl:for-each&gt; 
      
        &lt;xsl:text&gt;&amp;#10;&lt;/xsl:text&gt; 
    &lt;/xsl:for-each&gt;   
    

    </xsl:template>

    </xsl:stylesheet>

    After the change, I want to get.

    a/Item[@key='1']/c1/d11/d12/@value
    a/b2/Item[@key='fix']/d21/d22/@value
    a/b3/c3/Item[@key='price']/e2/@value

    But it's been made after the transformation:

    a/Item[@key='1']/c1/d11/d12/@valued13
    a/b2/Item[@key='fix']/d21/d22/@valued23
    a/b3/c3/Item[@key='price']/e2/@valuee3

    How to filter the unnecessary nodes after the attribution @value?



  • I just took out the extra code:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="text"/>
      <xsl:template match="/">
        <xsl:for-each select="//*[@value]">
          <xsl:for-each select="ancestor-or-self::*">
            <xsl:value-of select="name()"/>
            <xsl:text>/</xsl:text>
          </xsl:for-each>
          <xsl:text>@value&#10;</xsl:text>
        </xsl:for-each>
      </xsl:template>
    </xsl:stylesheet>
    

    Result:

    a/Item/c1/d12/@value
    a/b2/Item/d22/@value
    a/b3/c3/Item/e2/@value
    

    If necessary, the code that removes the attribute from the component may be returned to the location. Item:

    <xsl:if test ="name()='Item'">
      <xsl:text>[@key='</xsl:text>
      <xsl:value-of select="@key"/>
      <xsl:text>']/</xsl:text>
    </xsl:if>
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2