Skip to content
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

XML Parsing Help #40

Open
seansms opened this issue Sep 18, 2012 · 3 comments
Open

XML Parsing Help #40

seansms opened this issue Sep 18, 2012 · 3 comments

Comments

@seansms
Copy link

seansms commented Sep 18, 2012

For some reason, I can't get this test to pass.

    [Test]
    public void Sean_RenderXMLContent()
    {
        string XMLContent = @"<group><couponid>123</couponid><couponid>124</couponid></group>";
        string template2 = @"{{#group}}{{couponid}}{{/group}}";
        string expectedHTML = @"123124";
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(XMLContent);
        object data = doc.DocumentElement;
        var output = Render.StringToString(template2, data);
        Assert.AreEqual(expectedHTML, output);
    }

Any ideas what I'm doing wrong?

@abrinsmead
Copy link

I have the same issue. I don't think you are doing anything wrong.

@Pxtl
Copy link

Pxtl commented Mar 30, 2016

Following up on this since I've been tinkering with Nustache, the XML behavior is weak and poorly defined and I'm tinkering with improving it. @seansms is probably never going to read this 3.5 years later, but we'll pretend he will.

Remember that, since <couponid> is repeated, it is therefore a collection, not an individual value.

This works:

    [Test]  
    public void Sean_RenderXMLContent()  
    {  
        string XMLContent = @"<group><couponid>123</couponid><couponid>124</couponid>  </group>";  
        string template2 = @"{{#group}}{{#couponid}}{{.}}{{/couponid}}{{/group}}";
        string expectedHTML = @"123124";  
        XmlDocument doc = new XmlDocument();  
        doc.LoadXml(XMLContent);  
        object data = doc; 
        var output = Render.StringToString(template2, data);  
        Assert.AreEqual(expectedHTML, output);  
    }

Two things were wrong in your example:

object data = doc.DocumentElement;

This means the contextual object is now "group". If you want the unnamed root of your entire XML snippet so you can reference the root object by name (group, in this case) you just use object data = doc;

Otherwise, you should be leaving "group" out of your template and assuming that group is the default context.

Second, because couponid is repeated, it's actually a collection of nameless strings. Group isn't the array, couponid is - group effectively has only one child. I know, it's weird.

Now, the problem is that nustache xml getters don't support fetching grandchildren of xml objects - they only support either returning Xml Node Lists (which you can only iterate across, not index) or strings. So we can't say group.couponid or couponid.1 to get the first couponid.

I've got some PRs planned to fix that.

Edit: I'm silly. It takes xpath queries.

string template2 = @"{{#group/couponid}}{{.}}{{/group/couponid}}";

@Pxtl
Copy link

Pxtl commented Mar 31, 2016

Sorry for calling the XML behavior weak - it is, at worst, counterintuitive. Xpath queries have as much if not more expressive power as Mustache dot paths. It's very inconsistent to switch to the xpath syntax, but after tinkering for a bit I can see that it works.

But it does seem to fail on cdata.

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