Change tag names and remove element for a certain value

Am not able to traverse child element. In the below input XML, I have to take only element <e> and replace that with <se> and need to remove one element <e> if value equals to "DB1". Can someone help on this please? Am new to XSLT.

Input XML:

<a xmlns="http://examle.com/test/2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" >
  <b>
     <c name="RES" type="KSD">
     <d>
        <e>DB1</e>
        <e>DB2</e>
        <e>DB3</e>      
     </d>
    </c>
  </b>
  <error count="0" success="OK">
  </error>
</a>

The desired output XML is:

<?xml version="1.0" encoding="utf-8" ?>
<Payload xmlns="http://example.com/test/2.0"xmlns:xs="http://www.w3.org/2001/XMLSchema" >
    <response>
        <allot>8</allot>            
        <size>200</size>
        <ses>
            <se>DB2</se>
            <se>DB3</se>
        </ses>          
    </response>
</Payload>

Answers


The solution that seems to me to be closest to your needs (quite ambiguous) is

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" />

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

    <!-- rename -->
    <xsl:template match="a">
      <Payload>
        <xsl:apply-templates select="@*|node()"/>
      </Payload>
    </xsl:template>

    <!-- rename and add some new elements -->
    <xsl:template match="b">
      <response>
        <allot>8</allot>            
        <size>200</size>
        <xsl:apply-templates select="@*|node()"/>
      </response>
    </xsl:template>

    <!-- don't output this node but only its children -->
    <xsl:template match="c">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:template>

    <!-- rename -->
    <xsl:template match="d">
      <ses>
        <xsl:apply-templates select="@*|node()"/>
      </ses>
    </xsl:template>

    <!-- rename -->
    <xsl:template match="e">
      <se>
        <xsl:apply-templates select="@*|node()"/>
      </se>
    </xsl:template>

    <!-- discard -->
    <xsl:template match="e[.='DB1']|error"/>

</xsl:stylesheet>

I can't deal with namespaces so I removed xmlns namespace from the input and it worked.


I have written XSLT tranformation like this..

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" indent="yes" />
  <xsl:strip-space elements="*" />

  <xsl:template match="@*|comment()|processing-instruction()|text()">
    <xsl:copy-of select="."/>
  </xsl:template>

  <xsl:template match="*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="d" >
    <Payload xmlns="http://example.com/test/2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" >
      <response>
        <allot>8</allot>
        <size>200</size>
        <ses>
          <xsl:for-each select="e">
            <xsl:variable name="e_name" select="."></xsl:variable>
            <xsl:if test="$e_name != 'DB1'">
              <se>
                <xsl:value-of select="." />
              </se>
            </xsl:if>
          </xsl:for-each>
        </ses>
      </response>
    </Payload>
  </xsl:template>

</xsl:stylesheet>

Need Your Help

UITableViewController swift different sizes of height with images

ios swift uitableview

I have an UITableViewController that has 2 cells and when I put a photo in it, it comes out to be the same size. The coding I put in the ViewController.swift is

iPhone SDK: How to generate a random time of day

objective-c iphone xcode

I am having some problems trying to figure out the best way of generating a random time of day. For instance, if I want to choose a random time between the hours of 9am and 5pm, what would be the b...