How can I add quotes to attributes

22 views
Skip to first unread message

wbrisett

unread,
May 10, 2023, 4:31:41 PM5/10/23
to nokogiri-talk
I’ve got a project that requires me to make some modifications to SVG files.

I have tried a few things including trying to encode using the HTML and XML fragment from a string, but in every case I end up with this:

<text x="484" y="47.5" style="&quot;fill:black&quot; &quot;font-family:monospace&quot; &quot;font-size:16&quot; text-anchor=&quot;middle&quot; alignment-baseline=&quot;central&quot;">RES0</text>

What I am trying to do is replace the existing style attribute so I’ve been using this:

node.set_attribute(name,string)

However, it’s that string part that won’t work either by trying to encode it using fragment or leaving it as a  In this particular case the string I’m needed to be part of that style attribute normally looks like this:

"fill:black" "font-family:monospace" "font-size:16" text-anchor="middle" alignment-baseline="central"

So ultimately my <text> element would look like this:

<text x="484" y="47.5" style="fill:black" "font-family:monospace" "font-size:16" text-anchor="middle" alignment-baseline="central">RES0</text>

I’ve tried using single quotes but those stay single quotes. I’m sure there’s something simple I’m forgetting (I don’t usually have attributes that have values stacked like this and needing quotes like this).

-Wayne

Walter Lee Davis

unread,
May 10, 2023, 5:13:26 PM5/10/23
to nokogiri-talk
The result you want is not valid XML, so Nokogiri will not do that for you. The correct way that your text element should appear is this:

<text x="484" y="47.5" style="fill:black; font-family:monospace; font-size:16" text-anchor="middle" alignment-baseline="central">RES0</text>

The "style" part is meant to be one attribute, with semicolons separating the individual parts within it.

I'm not familiar with the properties `text-anchor` and `alignment-baseline`, but if they are supposed to become part of the style attribute, then you'd need to add them to that string somehow. If the result of this is not meant to be CSS-inlined in HTML (and the `<text>` tag is kind of a strong indicator that you are not building HTML here), then perhaps you are intending to pull the existing style attribute's nested values out so they can join the other attributes of your text tag.

Could you please clarify your goal here? I can offer additional suggestions once it is more clear what you expect the result to be, and whether it should be valid XML in the end. (Spoilers: Nokogiri only makes valid XML.)

Walter
> --
> You received this message because you are subscribed to the Google Groups "nokogiri-talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nokogiri-tal...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/nokogiri-talk/91bf22df-6b6f-41fe-ae10-4910d65d6c9cn%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages