Search code examples
xsltgrepxmlstarlet

update xsl file with xmlstaret, grep or similar


I have a series of similar XSL files and to all of them, I have to add the same XSL element in a specific position.

Here you can find a portion of the XSL to be updated and the element to be inserted is <xsl:call-template name="distributor.xsl"/> and it has to be after the </mrd:distributionFormat> and before the <mrd:transferOptions> tags

Is there a way to automatize this update to all my XSL files using XmlStarlet grep or similar?

...
<mdb:distributionInfo>
      <mrd:MD_Distribution>
         <mrd:distributionFormat>
            <mrd:MD_Format>
               <mrd:formatSpecificationCitation>
                  <cit:CI_Citation>
                     <cit:title>
                        <gco:CharacterString>WCS</gco:CharacterString>
                     </cit:title>
                     <cit:date gco:nilReason="unknown"/>
                     <cit:edition>
                        <gco:CharacterString>2.0</gco:CharacterString>
                     </cit:edition>
                  </cit:CI_Citation>
               </mrd:formatSpecificationCitation>
            </mrd:MD_Format>
         </mrd:distributionFormat>
                 <!-- call-template  -->           
                <xsl:call-template name="distributor.xsl"/>
                 <!-- call-template -->
         <mrd:transferOptions>
         ...

I tried with

xmlstarlet ed -P -S -L -s //mrd:MD_Distribution -t elem -i xsl:include -t attr -n "name" -v "distributor.xsl" main.xsl where main.xsl is the file to be updated


Solution

  • After changed requirements

    Following commmand inserts the desired xsl:call-template node where you want (for details see my first posting),

    xmlstarlet edit \
        -i '//mrd:transferOptions[1]' \
        -t elem -n 'xsl:call-template' -v '' \
        -s '$prev' -t attr -n name -v 'distributor.xsl' \
    main.xsl
    

    when run on the following XML file (adjust namespaces as needed):

    <xsl:transform version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:cit="urn:so70244776_cit"
      xmlns:gco="urn:so70244776_gco"
      xmlns:mdb="urn:so70244776_mdb"
      xmlns:mrd="urn:so70244776_mrd"
    >
      <xsl:template name="x">
        <mdb:distributionInfo>
            <mrd:MD_Distribution>
               <mrd:distributionFormat>
                  <mrd:MD_Format>
                     <mrd:formatSpecificationCitation>
                        <cit:CI_Citation>
                           <cit:title>
                              <gco:CharacterString>WCS</gco:CharacterString>
                           </cit:title>
                           <cit:date gco:nilReason="unknown"/>
                           <cit:edition>
                              <gco:CharacterString>2.0</gco:CharacterString>
                           </cit:edition>
                        </cit:CI_Citation>
                     </mrd:formatSpecificationCitation>
                  </mrd:MD_Format>
               </mrd:distributionFormat>
               <mrd:transferOptions/>
            </mrd:MD_Distribution>
        </mdb:distributionInfo>
      </xsl:template>
    
    </xsl:transform>