0

I was wording how I could reorder the tag Rice:

  • Move the tag <Rice>long-grain</Rice> to down and ***<Rice>123</Rice>*** to up;
  • Remove the third tag <Rice> or remove it in case it is empty like <Rice/> using XSLT:
    //Original XML
    <Company>
        <FarmName addressID="123465789">
            <Name xml:lang="en">Main Farm</Name>
            <Details>
                <Milk>Jodie Forster</Milk>
                <Bajra>ESRKSC</Bajra>
                <Rice>long-grain</Rice> //move to down
                <Rice>123</Rice> // move to up
                <Rice/> //remove if it is empty
                <Jowar>852</Jowar>
                <Lobia>WW</Lobia>
                <Maize>5240</Maize>
                <Masoor isoCode="US">123</Masoor>
            </Details>
            <Email>[email protected]</Email>
            <Phone>
                <TelephoneNumber>
                    <AreaOrCityCode/>
                    <Number>123456789<Number/>
                </TelephoneNumber>
            </Phone>
        </FarmName>
    </Company>
    
    //Desire XML
    <Company>
        <FarmName addressID="123465789">
            <Name xml:lang="en">Main Farm</Name>
            <Details>
                <Milk>Jodie Forster</Milk>
                <Bajra>ESRKSC</Bajra>
                <Rice>123</Rice>
                <Rice>long-grain</Rice>
                <Jowar>852</Jowar>
                <Lobia>WW</Lobia>
                <Maize>5240</Maize>
                <Masoor isoCode="US">123</Masoor>
            </Details>
            <Email>[email protected]</Email>
            <Phone>
                <TelephoneNumber>
                    <AreaOrCityCode/>
                    <Number>123456789<Number/>
                </TelephoneNumber>
            </Phone>
        </FarmName>
    </Company>

Thanks

1
  • Your example is ambiguous. Please explain the rule by which you want the Rice elements to be ordered. Commented Dec 6, 2022 at 9:08

1 Answer 1

1

To switch the position of the first and second <Rice> element you can use the following XSLT-1.0 stylesheet:

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

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

    <xsl:template match="Rice" />           <!-- Remove all Rice elements (except those selected by more specific templates)-->

    <xsl:template match="Rice[1]">          <!-- Select only the first Rice element -->
      <xsl:copy-of select="../Rice[2]" />   <!-- Switch first and second -->
      <xsl:copy-of select="../Rice[1]" />   
      <xsl:copy-of select="../Rice[position()>2 and normalize-space()]" />  <!-- Copy the (non-empty) rest -->
    </xsl:template>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.