How can I make empty tags self-closing with Nokogiri?

后端 未结 2 1810
别跟我提以往
别跟我提以往 2021-01-18 23:18

I\'ve created an XML template in ERB. I fill it in with data from a database during an export process.

In some cases, there is a null value, in which case an element

相关标签:
2条回答
  • 2021-01-18 23:54

    Search for

    <([^>]+)>\s*</\1>
    

    and replace with

    <\1/>
    

    In Ruby:

    result = subject.gsub(/<([^>]+)>\s*<\/\1>/, '<\1/>')
    

    Explanation:

    <       # Match opening bracket
    (       # Match and remember...
     [^>]+  # One or more characters except >
    )       # End of capturing group
    >       # Match closing bracket
    \s*     # Match optional whitespace & newlines
    <       # Match opening bracket
    /       # Match /
    \1      # Match the contents of the opening tag
    >       # Match closing bracket
    
    0 讨论(0)
  • 2021-01-18 23:54

    A couple questions:

    1. <foo></foo> is the same as <foo />, so why worry about such a tiny detail? If it is syntactically significant because the text node between the two is a "\n", then put a test in your ERB template that checks for the value that would go there, and if it's not initialized output the self-closing tag instead? See "Yak shaving".
    2. Why involve Nokogiri? You should be able to generate correct XML in ERB since you're in control of the template.

    EDIT - Nokogiri's behavior is to not-rewrite parsed XML unless it has to. I suspect you'd have to remove the node in question, then reinsert it as an empty node to get Nokogiri to output what you want.

    0 讨论(0)
提交回复
热议问题