1

I'm trying to remove attribute whose value ="remove_it" but not able to find a way

This is Input xml

<row>                   
        <entry>
                                <p type="some1">
                                    <t>This is text</t>
                                </p>
                            </entry>
                            <entry>
                                <p type="remove_it">
                                    <t> </t>
                                </p>
                            </entry>
                            <entry>
                                <p type="remove_it">
                                    <t> </t>
                                </p>
                            </entry>
                            <entry>
                                <p type="remove_it">
                                    <t> </t>
                                </p>
                            </entry>
                        </row>
                        <row>
                            <entry>
                                <p type="remove_it">
                                    <t> </t>
                                </p>
                            </entry>
                            <entry>
                                <p type="remove_it">
                                    <t> </t>
                                </p>
                            </entry>
                            <entry>
                                <p type="remove_it">
                                    <t> </t>
                                </p>
                            </entry>
                            <entry>
                                <p type="remove_it">
                                    <t> </t>
                                </p>
                            </entry>
                        </row>
                        <row>
                            <entry>
                                <p type="remove_it">
                                    <t> </t>
                                </p>
                            </entry>
                            <entry>
                                <p type="remove_it">
                                    <t> </t>
                                </p>
                            </entry>
                            <entry>
                                <p type="remove_it">
                                    <t> </t>
                                </p>
                            </entry>
                            <entry>
                                <p type="remove_it">
                                    <t> </t>
                                </p>
                            </entry>
                        </row>
                    </tbody>
                </tgroup>
            </table>
            <p>
                <t> </t>
            </p>
        </body>
    </section>
    

want something like

        <tgroup>
        <tbody>
            <row>
                <entry>
                    <p type="some1">
                        <t>This is text</t>
                    </p>
                </entry>
                <entry>
                    <p>
                        <t> </t>
                    </p>
                </entry>
                <entry>
                    <p>
                        <t> </t>
                    </p>
                </entry>
                <entry>
                    <p>
                        <t> </t>
                    </p>
                </entry>
            </row>
            <row>
                <entry>
                    <p>
                        <t> </t>
                    </p>
                </entry>
                <entry>
                    <p>
                        <t> </t>
                    </p>
                </entry>
                <entry>
                    <p>
                        <t> </t>
                    </p>
                </entry>
                <entry>
                    <p>
                        <t> </t>
                    </p>
                </entry>
            </row>
            <row>
                <entry>
                    <p>
                        <t> </t>
                    </p>
                </entry>
                <entry>
                    <p>
                        <t> </t>
                    </p>
                </entry>
                <entry>
                    <p>
                        <t> </t>
                    </p>
                </entry>
                <entry>
                    <p>
                        <t> </t>
                    </p>
                </entry>
            </row>
        </tbody>
    </tgroup>
    </table>
    <p>
        <t> </t>
    </p>
    </body>
    </section>
    

what i have been trying

          <xsl:template match="sc:entry[(child::*[local-name()='p'][@type='_para'])]">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
      <xsl:template match="sc:entry[(child::*[local-name()='p'][@type='_para'])]">

but this remove the entire node, I just want to remove attribute of p whose parent is entry
Is there any way I can only remove attribute of those value = "remove_it" Thanks in advance!!

1 Answers1

1

I just want to remove attribute of p whose parent is entry

Consider this example:

XML

<root>
    <row>
        <entry>
            <p type="some1">
                <t>This is text</t>
            </p>
        </entry>
        <entry>
            <p type="remove_it">
                <t/>
            </p>
        </entry>
        <entry>
            <p type="remove_it">
                <t/>
            </p>
        </entry>
    </row>
    <row>
        <entry>
            <p type="remove_it">
                <t/>
            </p>
        </entry>
        <entry>
            <p type="keep_it">
                <t/>
            </p>
        </entry>
        <entry>
            <p type="remove_it">
                <t/>
            </p>
        </entry>
        <entry>
            <p type="remove_it">
                <t/>
            </p>
        </entry>
    </row>
</root>

XSLT 3.0

<xsl:stylesheet version="3.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="*"/>

<xsl:mode on-no-match="shallow-copy"/>

<xsl:template match="entry/p/@type[.='remove_it']"/>

</xsl:stylesheet>

Result

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <row>
      <entry>
         <p type="some1">
            <t>This is text</t>
         </p>
      </entry>
      <entry>
         <p>
            <t/>
         </p>
      </entry>
      <entry>
         <p>
            <t/>
         </p>
      </entry>
   </row>
   <row>
      <entry>
         <p>
            <t/>
         </p>
      </entry>
      <entry>
         <p type="keep_it">
            <t/>
         </p>
      </entry>
      <entry>
         <p>
            <t/>
         </p>
      </entry>
      <entry>
         <p>
            <t/>
         </p>
      </entry>
   </row>
</root>

Note:

You should never have to use a hack like *[local-name()='p']. If your input nodes are in a namespace, then deal with the namespace properly - see: XSLT Transform doesn't work until I remove root node.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51