how to change the tag name in xslt?

2019-09-15 04:25发布

问题:

could you please tell me how to change the tag name xslt ? ?I want to changeimg tag to imp-img tag . here is my code http://xsltransform.net/bwdwsT/1

Expected out

<!DOCTYPE html
  PUBLIC "XSLT-compat">
<hmtl>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>New Version!</title>
   </head>
   <aa>
            <div class="section1">
         <div class="Normal">
            <p>
               <imp-img height="260" alt="" hspace="6" width="310" align="left" vspace="6" src="/photo/a.cms">
               ffff<br>
               <br>
               hh<br>
               <br>
               vvggg<br>
               <br>
               vv<br>
               <br>
               ftr<br>
               <br>
               fff
            </p>

         </div>

      </div>

   </aa>
</hmtl>

`Transformation code

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="/">
      <hmtl>
        <head>
          <title>New Version!</title>
        </head>
        <xsl:apply-templates/>
      </hmtl>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:transform>

current Output

<!DOCTYPE html
  PUBLIC "XSLT-compat">
<hmtl>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>New Version!</title>
   </head>
   <aa>

      <div class="section1">

         <div class="Normal">

            <p>
               <span class="Drop-Film" multiline="0">
                  <span class="italic" multiline="0">
                     \xxxcc
                     </span>
                  </span>

            </p>

            <p>
               xxx
            </p>

            <p>
               xxxx
            </p>

         </div>

      </div>

   </aa>
</hmtl>

updated

http://xsltransform.net/bwdwsT/2

回答1:

You can also use axis to change tag only when has specific ancestor - in your case aa. Just use ancestor::aa in the selector

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <hmtl>
            <head>
                <title>New Version!</title>
            </head>
            <xsl:apply-templates/>
        </hmtl>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="imp-img[ancestor::aa]">
        <img>
            <xsl:apply-templates select="@*|node()"/>
        </img>
    </xsl:template>
</xsl:transform>


回答2:

Here http://xsltransform.net/bwdwsT/5

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="/">
      <hmtl>
        <head>
          <title>New Version!</title>
        </head>
        <xsl:apply-templates/>
      </hmtl>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="aa/div/div/p/imp-img">          
        <img><xsl:apply-templates select="@*|node()" /></img>
    </xsl:template>


</xsl:transform>

it based on this How do I rename XML tags using XSLT

Update 1

New version of selector