Menu

#108 another empty value where changed too

all
closed-wont-fix
nobody
None
5
2019-02-26
2014-07-16
Peter
No

Two values in the xml file

<ConfigData>
  <parameter>
    <name>UserName</name>
    <value></value>
  </parameter>
  <parameter>
    <name>Password</name>
    <value></value>
  </parameter>
</ConfigData>

xml edit -L -N x="namespace" -u "/x:ConfigData/x:parameter[x:name='UserName']/x:value" -v "test" "filename.xml"

will result in the following change:

<ConfigData>
  <parameter>
    <name>UserName</name>
    <value>test</value>
  </parameter>
  <parameter>
    <name>Password</name>
    <value/>
  </parameter>
</ConfigData>

Discussion

  • Noam Postavsky

    Noam Postavsky - 2014-07-16

    As far as XML in concerned <value></value> is exactly the same as <value/>, so xmlstarlet can't see the difference.

     
  • Noam Postavsky

    Noam Postavsky - 2014-07-16
    • status: open --> closed-wont-fix
     

    Last edit: Noam Postavsky 2014-07-16
  • Trevor North

    Trevor North - 2016-12-27

    Whilst it is true that in XML there is no difference, unfortunately lots of developers write configuration files without knowing this fact. Therefore it would be useful if it was optional whether it outputs empty tag pairs or a single self closing tag.

    As i am doing inplace edits of existing XML files, i had hoped the Preserve Format option would just leave everything alone apart from the tags i was changing but that seems to be not the case as any empty tag pairs were changed.

    Please reconsider changing the status on this issue and make it open rather than closed wont fix.

    Thanks

     
    • Noam Postavsky

      Noam Postavsky - 2016-12-27

      I didn't mark it as wontfix because I disagree that it's useful. Rather, implementing this would essentially require rewriting xmlstarlet from scratch, and/or significant redesign of libxml. There's no realistic chance of it ever happening.

       
  • Trevor North

    Trevor North - 2016-12-28

    Really ? - i've been looking around the web and it suggests, it is the serialiser that makes this change but in those cases, passing in xsl:output method="html" was suggested as a fix. As there isnt a -C option to output the xslt generated for edit mode (although this would also be useful), i cannot tell if this would work or not (if it was available, i could at least tell if this workaround would have the desired effect)

    http://p2p.wrox.com/xslt/44616-how-force-xslt-output-close-tag-xml.html

    In the meantime, for others that might have the same issue, i've created this sed command to return any self closing tags to empty pairs which can be run after xmlstarlet. It deals with tags that have multiple attributes and namespaces, as well as those that have none.

    sed -i 's|<([^ ]) (.)/>|<\1 \2></\1>|;s|<(.*)/>|<\1></\1>|' filename

     
    • Noam Postavsky

      Noam Postavsky - 2016-12-28

      Oh, I was interpreting the request as preserving the close vs self closing from the input. Making the output use all self closing or all non-self closing regardless of input form might indeed be doable.

      As there isnt a -C option to output the xslt generated for edit mode

      Yeah, that's because edit is not currently implemented with xslt.

       
  • Trevor North

    Trevor North - 2019-02-26

    Seems i hit the same issue and in searching for the fix, came across this old bug report.
    I tried to use my original sed but seems the comment lost something in translation (at least the ([^ ]) should have a tab in there but still it doesnt work and i cannot remember how it should be.

    Luckily, i found this alternative below which works so just wanted to update here in case anyone else is looking for this fix.

    sed -E 's#<([^/ ]+)/>#<\1></\1>#g' filename.xml > filename,new

    From https://unix.stackexchange.com/questions/411745/how-to-change-empty-xml-tags-in-a-file-in-unix

     

Log in to post a comment.