5kg

How to execute XSL to remove text from xml tags

I have XML:

<?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 so that the tag <weight>needs to be converted to Weightand takes into account a single number. With this, I want to add an additional tag that is missing in the input XML. <unitWeights>if the value <weight>is only presnt, and only then it will show the remnants of KG, it will be empty.

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 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 where I am wrong.

+4
source share
2 answers
<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>
0
source

Below is a general solution for your requirement.

  • Only for node that is "weight", you need to process or apply the template.

    <xsl:template match="weight">
    
  • translate, , .  XPath -

: 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>
+1

Source: https://habr.com/ru/post/1662217/


All Articles