xslt how to remove the parent tag and change attributes of a child tag in an xml doc

geek1209

I have an example xml like below

<map>
<topicref href="disclaimer_PUBLIC.dita" outputclass="Top">
    <fig>
      <image href="">
        qw
      </image>
      <image href="">
        q1
      </image>
    </fig>
  </topicref>
  <topicref href="DocID030323.xml">
    <fig>
      <image href="">
        qw
      </image>
      <image href="">
        q1
      </image>
    </fig>
  </topicref>
</map>

Now here I want to remove the tag <fig> and add attribute width=5cm to the underlying image tag whenever the topicref outputclass="Top" . I want the output as below:

<topicref href="../Topics/dita" outputclass="Top">

      <image href="" width="5cm">
        qw
      </image>
      <image href="" width="5cm">
        q1
      </image>

  </topicref>
  <topicref href="DocID030323.dita">
      <fig class="- topic/fig ">
         <image href=""
        qw
      </image>
         <image href="">
        q1
      </image>
      </fig>
  </topicref>

I am trying to achieve this but can only do 1 operation at a time in an xslt. how to combine both operations?

when I use

<xsl:template match="topicref[@outputclass='Top']/fig" >
      <xsl:apply-templates select="node()"/>

  </xsl:template> 

the fig element is removed but can't change the attribute values.

and when I use

<xsl:template match="topicref[@outputclass='Top']/fig/image" >
    <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:attribute name="width">
      <xsl:value-of select="'2.5'"/>
    </xsl:attribute>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>

  </xsl:template>

even the fig element is copied.I want to perform both operations in 1 xslt. Please help.

Erik Zander

The following XSLT

  <xsl:template match="topicref[@outputclass='Top']/fig">
    <xsl:apply-templates></xsl:apply-templates>
</xsl:template>
<xsl:template match="topicref[@outputclass='Top']/fig/image" >
    <xsl:copy>

        <xsl:copy-of select="@*"/>
        <xsl:attribute name="width">
            <xsl:value-of select="'2.5'"/>
        </xsl:attribute>
        <xsl:apply-templates select="node()"/>
    </xsl:copy>

</xsl:template>
<xsl:template match="*">
    <xsl:copy>
        <xsl:copy-of select="@*"></xsl:copy-of>
        <xsl:apply-templates></xsl:apply-templates>
    </xsl:copy>
</xsl:template>

Produces

<?xml version="1.0" encoding="UTF-8"?><map>
<topicref href="disclaimer_PUBLIC.dita" outputclass="Top">

        <image href="" width="2.5">
            qw
        </image>
        <image href="" width="2.5">
            q1
        </image>

</topicref>
<topicref href="DocID030323.xml">
    <fig>
        <image href="">
            qw
        </image>
        <image href="">
            q1
        </image>
    </fig>
</topicref>
</map>

Hope this helps

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to properly remove an XML tag using XSLT

How to get and convert attributes after tag <product to xml | XSLT

How to remove the anchor tag from parent class not from child class?

Move tag to child element xml - xslt

Wrap XML sibling tags in a parent tag with XSLT

How to remove XML tag based on child attribute using php?

Get value from parent and child tag from a xml file using XSLT

How to remove parent tag name?

How can i add / remove a class to a child tag with different parent tag

How to comment out a parent xml tag with a specific child tag using sed?

xml.etree.ElementTree append parent tag to child tag

Remove xml tag and associated child with xsl file

select parent of a tag with XSLT

Sorting parent tags by using child tag values in XSLT

XSLT copy parent element with attributes and change child elements

how remove tag (which contains special tag ) via xslt

How to get value of XML tag with different attributes?

How to wrap child elements in a parent tag?

How to get the text only from parent tag and not from the child tag

How to get the text from a child tag onChange on the parent tag?

How to insert (append) a string containing XML as XML into inner XML (or remove a parent tag but keep the content) with lxml in Python?

How to join elements with same tag within the same parent in Python XML despite their attributes?

XSLT:How to Maintain Sequential Order of tag in XML

How to read namespaced tag "<a:a>" from XML in XSLT?

How to Insert an XML tag after a particular tag using XSLT?

Xml doc - <see> tag on parameter

XSLT: How to avoid adding self closing tag in attributes in HTML

How to remove the parent element and move up the child element in XML using XSLT

How can to change same css style after hover parent to child tag element?