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

How to quickly split an element into multiple elements? #429

Open
NodeGreenHand opened this issue Mar 4, 2021 · 16 comments
Open

How to quickly split an element into multiple elements? #429

NodeGreenHand opened this issue Mar 4, 2021 · 16 comments
Assignees

Comments

@NodeGreenHand
Copy link

NodeGreenHand commented Mar 4, 2021

How to quickly split an element into multiple elements by taking a text substring in an element as a separating element
for example:
source element:<p><span><a href="void">0</a>123</span><span>45</span><span>678</span></p>
separate substrings:567
objective element:
<p><span><a href="void">0</a>123</span><span>4</span></p>
<p><span>5</span><span>67</span></p>
<p><span>8</span></p>

@JonathanMagnan JonathanMagnan self-assigned this Mar 4, 2021
@JonathanMagnan
Copy link
Member

Hello @NodeGreenHand ,

Your images don't work.

As asked when creating the issue, we would like to have a runnable example which at least starts your example, that will save us some time instead of redoing what you already did.

Best Regards,

Jon


Performance Libraries
context.BulkInsert(list, options => options.BatchSize = 1000);
Entity Framework ExtensionsEntity Framework ClassicBulk OperationsDapper Plus

Runtime Evaluation
Eval.Execute("x + y", new {x = 1, y = 2}); // return 3
C# Eval FunctionSQL Eval Function

@NodeGreenHand
Copy link
Author

Hello @JonathanMagnan

The problem has been revised. Please give me an idea, 3Q

Best Regards,

NodeGreenHand

@JonathanMagnan
Copy link
Member

Hello @NodeGreenHand ,

Besides doing something manually on your side, there is no method shortcut that allows you to do exactly what you want to do

@NodeGreenHand
Copy link
Author

Hello @JonathanMagnan

Q1:Which basic operation methods are the fastest in HAP?
Q2:
HtmlNode node = HtmlNode.CreateNode("<p><span>123</span></p>");
HtmlNode span = node.FirstChild;
HtmlNode newSpan = HtmlNode.CreateNode("<span></span>");
newSpan.AppendChild(span);
node.RemoveAllChildren();
node.AppendChild(newSpan);

Why? span.XPath The value of is wrong

Best Regards,

NodeGreenHand

@JonathanMagnan
Copy link
Member

Q1: Which basic operation methods are the fastest in HAP?

Your question is too much broad, I'm not sure to understand what you want.

Q2: Why? span.XPath The value of is wrong

What's wrong?

If you do span.XPath before you remove it, the value is: /p[1]/span[1]
If you do span.XPath after you remove it, the value is /span

@NodeGreenHand
Copy link
Author

Hell @JonathanMagnan

Sorry, I'm wrong. It's not the XPath error, but the parentnode value of the element. It shouldn't be null, it should be the parent element of the new replacement

Best Regards,

NodeGreenHand

@JonathanMagnan
Copy link
Member

Hello @NodeGreenHand ,

I'm not sure what you mean, could you explain it with some runnable code.

This code also looks as expected:

HtmlNode node = HtmlNode.CreateNode("<p><span>123</span></p>");
HtmlNode span = node.FirstChild;
HtmlNode newSpan = HtmlNode.CreateNode("<span></span>");
var xpath = newSpan.XPath;
newSpan.AppendChild(span);
node.RemoveAllChildren();
node.AppendChild(newSpan);
var xpath2= newSpan.XPath;

@NodeGreenHand
Copy link
Author

NodeGreenHand commented Mar 10, 2021 via email

@JonathanMagnan
Copy link
Member

Got it this time ;)

The problem is that you append an existing node to another parent, but the old parent keep referencing them, so when you do: pNode.RemoveAllChildren();, it removes children which is also under the aNode.

When appending an existing node, you should instead clone it.

For example, something like this:

HtmlNode aNode = HtmlNode.CreateNode("<a></a>");

// pNode.ChildNodes.ToList().ForEach(x => aNode.AppendChild(x.CloneNode(true)));
var childNodes = pNode.ChildNodes;
spanNode1 = childNodes[0].CloneNode(true);
spanNode2 = childNodes[1].CloneNode(true);

Console.WriteLine("the first child parent node is aNode:{0}; xPath is:{1}", aNode == spanNode1.ParentNode, spanNode1.XPath);
Console.WriteLine("the second child parent node is aNode:{0}; xPath is:{1}", aNode == spanNode2.ParentNode, spanNode2.XPath);

aNode.AppendChild(spanNode1);
aNode.AppendChild(spanNode2);

Since they are cloned, they are not the same node as the one referenced in the pNode.

So the output is now right:

the first child parent node is null:False; xPath is:/p[1]/a[1]/span[1]
the second child parent node is null:False; xPath is:/p[1]/a[1]/span[2]

Let me know if that explains correctly your current issue and the solution.

Best Regards,

Jon

@NodeGreenHand
Copy link
Author

Hello @JonathanMagnan

Although this idea can solve this problem, it is not my ideal solution.

In my application scenario, such operations are frequently performed. If deep copy is carried out every time, it will be time-consuming, and I need to be more efficient.

Therefore, in this case, can we change the logic in the removeallchildren() function to transfer the previously referenced element to another element directly, and all the data is correct

I think that's the best solution. What do you think?

Best Regards,
NodeGreenHand

@JonathanMagnan
Copy link
Member

We will look at it,

What you are asking is a kind of StealChildren method (I will have to come with a better name but you get the idea hehe).

@NodeGreenHand
Copy link
Author

Hello @JonathanMagnan

Should it be more efficient,hehe

Best Regards,
NodeGreenHand

@NodeGreenHand
Copy link
Author

Hello @JonathanMagnan

In fact, to put it simply, it is to add a new function. The function is to "insert a child element into the parent element and move all the child elements of the original parent element to the new child element at the same time."

In this new function, use the idea I said, so it's perfect

Best Regards,
NodeGreenHand

@JonathanMagnan
Copy link
Member

Hello @NodeGreenHand ,

The v1.11.32 has finally been released.

We added in this version the MoveChild and MoveChildren methods.

So instead of doing AppendChildren, you should now use MoveChildren if you wish that the node get also removed from the parent.

Let me know if that works as you would have wanted.

Best Regards,

Jon

@NodeGreenHand
Copy link
Author

Hello @JonathanMagnan

Thank you very much for taking my advice. I'll let you know if it's what I want after I test it.

Best Regards,
NodeGreenHand

@JonathanMagnan
Copy link
Member

Hello @NodeGreenHand ,

A simple reminder that we are here to assist you!

Don't hesitate to contact us once you test it!

Best regards,

Jon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants