Anand Deshmukh
Anand Deshmukh

Reputation: 113

How to perform XSL to remove the text from xml tags

I have a XML like :

<?xml version="1.0" encoding="UTF-8"?>
<COLLECTION>
<Added>
<part>
<weight>5kg</weight>

</part>
<part>
<weight></weight>
</part>
<part>
<weight>2kg</weight>
</part>
</Added>
<Update>
<part>
<weight></weight>
</part>
<part>
<weight>3kg</weight>
</part>
<part>
<weight>2kg</weight>
</part>
</Update>
</COLLECTION>

I want to apply XSL on it such that <weight> tag need to convert to Weight and consider the only number.With this I want to add some extra tag which is not present in input XML.<unitWeights> if the value of <weight> is presnt only and only then it will show KG rest it will be blank.

expected output:

<?xml version="1.0" encoding="UTF-8"?>
<COLLECTION>
<Added>
<part>
<Weight>5</Weight>
<unitWeights>KG</unitWeights>
</part>
<part>
<Weight></Weight>
<unitWeights></unitWeights>
</part>
<part>
<Weight>2</Weight>
<unitWeights>KG</unitWeights>
</part>
</Added>
<Update>
<part>
<Weight></Weight>
<unitWeights></unitWeights>
</part>
<part>
<Weight>3</Weight>
<unitWeights>KG</unitWeights>
</part>
<part>
<Weight>2</Weight>
<unitWeights>KG</unitWeights>
</part>
</Update>
</COLLECTION>

for this i have applied :

    <xsl:template match="Weight">
    <weight>
      <xsl:value-of select="translate(.,translate(., '0123456789', ''), '')"/>
    </weight>
  </xsl:template>
  <xsl:template match="WeightUnits">
    <unitWeights>KG</unitWeights>
    </xsl:template>

Please help me out where i am wrong.

Upvotes: 4

Views: 88

Answers (2)

Alexander Petrov
Alexander Petrov

Reputation: 14261

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

  <xsl:template match="weight">
    <Weight>
      <xsl:copy-of select="substring-before(.,'kg')"/>
    </Weight>

    <unitWeights>
      <xsl:copy-of select="substring(., string-length(.) - 1)"/>
    </unitWeights>
  </xsl:template>

  <xsl:template match="unitWeights"/>

</xsl:stylesheet>

Upvotes: 0

ScanQR
ScanQR

Reputation: 3820

Following is a generic solution for your requirement.

  1. For only node which 'weight', you need to process or apply template to.

    <xsl:template match="weight">
    
  2. Then you need to apply translate as mentioned in this to extract only string part and only digits parts. XPath - extract numeric value out of string

Demo for you : http://xsltransform.net/ejivdHb/23

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://locomotive/bypass/docx" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="weight">

    <xsl:variable name="currentWeight" select="translate(.,translate(., '0123456789', ''), '')" />
    <xsl:variable name="currentWeightUnit" select="translate(., '0123456789', '')" />

     <xsl:if test="string-length($currentWeight) &gt; 0">
         <Weight>
            <xsl:value-of select="$currentWeight"/>
        </Weight>
        <xsl:if test="string-length($currentWeightUnit) &gt; 0">
           <unitWeights>
             <xsl:value-of select="$currentWeightUnit"/>
           </unitWeights>
        </xsl:if>
      </xsl:if>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions