Home > OS >  Best way to add large number of tags to an xml?
Best way to add large number of tags to an xml?

Time:02-25

I need to add a considerable number of tags to an xml file. The file is the Openmediavault system (Debian) config.xml file.

Specifically I need to share a hard drive via SMB by command line. I've thought about doing it by SSH commands or a script in task scheduler. But the content that I have to add is quite extensive. How could I do it? Is there a way to do it using xmlstarlet? I haven't found easy ways to add so many tags. Let's see if you can give me an idea.

I have to put all these tags inside the <smb></smb> tag

<share>
          <uuid>0be7e06a-a888-436d-8088-5ec63963vf5</uuid>
          <enable>1</enable>
          <sharedfolderref>626db680-b317-42b9-a312-d30392bd5re4</sharedfolderref>
          <comment></comment>
          <guest>no</guest>
          <readonly>0</readonly>
          <browseable>1</browseable>
          <recyclebin>1</recyclebin>
          <recyclemaxsize>0</recyclemaxsize>
          <recyclemaxage>0</recyclemaxage>
          <hidedotfiles>1</hidedotfiles>
          <inheritacls>1</inheritacls>
          <inheritpermissions>0</inheritpermissions>
          <easupport>0</easupport>
          <storedosattributes>0</storedosattributes>
          <hostsallow></hostsallow>
          <hostsdeny></hostsdeny>
          <audit>0</audit>
          <timemachine>0</timemachine>
          <extraoptions></extraoptions>
 </share>

I have tried to add it in this way, in subnode.xml I have inserted what I wanted to add inside the shares tags, but it does not work, it shows me the content of the xml and does not edit it.

sudo xmlstarlet ed -a "//config/services/smb/shares/" -t elem -n share \ -v "$(xmlstarlet sel -t -c '//share/*' subnode.xml)" config.xml\ | xmlstarlet unesc

CodePudding user response:

Use an XSLT transformation:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:template match="*">
    <xsl:copy><xsl:apply-templates/></xsl:copy>
  </xsl:template>
  <xsl:template match="smb">
    <smb><xsl:copy-of select="document('newContent.xml')"/></smb>
  </xsl:template<
</xsl:transform>

The first template rule is a default rule; it says copy elements unchanged. The second rule overrides this for smb elements: it says insert a copy of newContent.xml into the smb element.

  • Related