hyperlink <a> title remove using xslt

I try to remove the tag title using XSLT. But it`s not removing. Can one can recommdate solution for remove anchor title.

XML File

<?xml version="1.0" encoding="utf-8"?>
<RichText xmlns="uuid:e25b1476-ce87-4a67-a22b-b82a752810e0">
  <Content>
    <a xmlns="http://www.w3.org/1999/xhtml" href="http://www.google.com" title="Google Title">
      Hyperlink
    </a>
  </Content>
</RichText>

XSLT File:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" method="xml" cdata-section-elements="script"></xsl:output>
  <xsl:template match="/ | node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*">
        </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="*[      (self::br or self::p or self::div)     and      normalize-space(translate(., &apos; &apos;, &apos;&apos;)) = &apos;&apos;     and      not(@*)     and      not(processing-instruction())     and      not(comment())     and      not(*[not(self::br) or @* or * or node()])     and      not(following::node()[not(         (self::text() or self::br or self::p or self::div)        and         normalize-space(translate(., &apos; &apos;, &apos;&apos;)) = &apos;&apos;        and         not(@*)        and         not(processing-instruction())        and         not(comment())        and         not(*[not(self::br) or @* or * or node()])       )])     ]">
    <!-- ignore all paragraphs and line-breaks at the end that have nothing but (non-breaking) spaces and line breaks -->
  </xsl:template>
  <xsl:template match="br[parent::div and not(preceding-sibling::node()) and not(following-sibling::node())]">
    <!-- Chrome generates <div><br/></div>. Renders differently in different browsers. Replace it with a non-breaking space -->
    <xsl:text> </xsl:text>
  </xsl:template>
</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="utf-8"?>
<RichText xmlns="uuid:e25b1476-ce87-4a67-a22b-b82a752810e0">
  <Content>
    <a xmlns="http://www.w3.org/1999/xhtml" href="http://www.google.com">
      Hyperlink
    </a>
  </Content>
</RichText>

2 answers

  • answered 2019-03-13 19:37 Alejandro

    If you are using the identity transformation, you need a rule for that attribute with an empty template.

    This input

    <RichText xmlns="uuid:e25b1476-ce87-4a67-a22b-b82a752810e0">
      <Content>
        <a xmlns="http://www.w3.org/1999/xhtml" href="http://www.google.com" title="Google Title">
          Hyperlink
        </a>
      </Content>
    </RichText>
    

    With this stylesheet

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xhtml="http://www.w3.org/1999/xhtml" >
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
      <xsl:template match="xhtml:a/@title"/>
    </xsl:stylesheet>
    

    Output

    <RichText xmlns="uuid:e25b1476-ce87-4a67-a22b-b82a752810e0">
      <Content>
        <a xmlns="http://www.w3.org/1999/xhtml" href="http://www.google.com">
          Hyperlink
        </a>
      </Content>
    </RichText>
    

    Note: look at the use of namespaces.

  • answered 2019-03-14 05:42 imran

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"  xmlns="uuid:e25b1476-ce87-4a67-a22b-b82a752810e0"
        xpath-default-namespace="http://www.w3.org/1999/xhtml"
        exclude-result-prefixes="xs"
        version="2.0">
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="a/@title"/>
    
    </xsl:stylesheet>
    You may also do using "xpath-default-namespace".