Skip to content

How to generate xml with innerxml tag? #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
donnol opened this issue Jan 9, 2024 · 3 comments
Open

How to generate xml with innerxml tag? #29

donnol opened this issue Jan 9, 2024 · 3 comments

Comments

@donnol
Copy link

donnol commented Jan 9, 2024

like:

	type Code struct {
		Text  string `xml:",innerxml"`
        }
@miku
Copy link
Owner

miku commented Jan 9, 2024

Currently, one cannot specify an element name (e.g. Code) that should not be parsed further. What you can do is to edit the generated struct by hand and change elements to innerxml.

Potentially, we could have a flag to say which elements should not be descended into further - but then you may have ambiguity (if two elements with the same name appear on different levels of the XML). Similar to the reason, why we have not added type inference just yet: at some point there will be ambiguity and one goal of zek was to keep it (relatively) simple to use (we already have 19 flags).

@3052
Copy link

3052 commented Mar 16, 2024

just ran into this. with this input:

<p>- Did you get me something to give her?<br/>- Yes, this.</p>

you get this result:

type P struct {
	XMLName xml.Name `xml:"p"`
	Text    string   `xml:",chardata"`
	Br      string   `xml:"br"`
} 

which strips the internal <br> element from the text. maybe the code could include a comment, something like:

type P struct {
	XMLName xml.Name `xml:"p"`
	Text    string   `xml:",chardata"` // or innerxml
	Br      string   `xml:"br"`
} 

@miku
Copy link
Owner

miku commented Mar 16, 2024

Thanks for the issue report.

I added a new flag -I to zek to change the tag generated (default is still chardata) - you'll find it in v0.1.24. Example:

$ cat testdata/w.16.xml
<!-- https://github.com/miku/zek/issues/29#issuecomment-2001630625 -->
<p>- Did you get me something to give her?<br/>- Yes, this.</p>

$ zek -I < testdata/w.16.xml 
// P was generated 2024-03-16 11:52:25 by tir on reka with zek 0.1.24.
type P struct {
        XMLName xml.Name `xml:"p"`
        Text    string   `xml:",innerxml"`
        Br      string   `xml:"br"`
} 

This is a tradeoff: simple flag, but no fine-granular control.

miku added a commit that referenced this issue Aug 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants