删除 xml 元素后删除空白行?

发布于 2024-08-26 14:22:40 字数 1331 浏览 3 评论 0原文

我用 simpledom 从 xml 文件中删除了一些元素。

代码:

$this->xmlDocument->removeNodes("//entity[name='mac']");

这是初始文件:

<entity id="1000070">
    <name>apple</name>
    <type>category</type>
    <entities>
        <entity id="7002870">
            <name>mac</name>
            <type>category</type>
        </entity>
        <entity id="7024080">
            <name>iphone</name>
            <type>category</type>
        </entity>
        <entity id="7024080">
            <name>ipad</name>
            <type>category</type>
        </entity>
    </entities>
</entity>

之后的文件:

<entity id="1000070">
    <name>apple</name>
    <type>category</type>
    <entities>




        <entity id="7024080">
            <name>iphone</name>
            <type>category</type>
        </entity>
        <entity id="7024080">
            <name>ipad</name>
            <type>category</type>
        </entity>
    </entities>
</entity>

我想知道如何删除运行删除代码后留下的空白行?

谢谢!

i removed some elements from a xml file with simpledom.

the code:

$this->xmlDocument->removeNodes("//entity[name='mac']");

here is the initial file:

<entity id="1000070">
    <name>apple</name>
    <type>category</type>
    <entities>
        <entity id="7002870">
            <name>mac</name>
            <type>category</type>
        </entity>
        <entity id="7024080">
            <name>iphone</name>
            <type>category</type>
        </entity>
        <entity id="7024080">
            <name>ipad</name>
            <type>category</type>
        </entity>
    </entities>
</entity>

the file afterwards:

<entity id="1000070">
    <name>apple</name>
    <type>category</type>
    <entities>




        <entity id="7024080">
            <name>iphone</name>
            <type>category</type>
        </entity>
        <entity id="7024080">
            <name>ipad</name>
            <type>category</type>
        </entity>
    </entities>
</entity>

i wonder how i could also remove the blank lines that are left after i ran the removal code?

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

书间行客 2024-09-02 14:22:40

您可以使用 XPath 将空白文本节点作为目标。

如果要删除的节点是 //entity[name='mac'] 那么它前面的空白文本节点就是 然后

//text()[normalize-space(.) = ''][following-sibling::entity[position()=1][name='mac']]

,您可以使用 |< 删除两个节点集/code> 运算符,您的命令将变为:

$this->xmlDocument->removeNodes(
    "//entity[name='mac'] | //text()[normalize-space(.) = ''][following-sibling::entity[position()=1][name='mac']]"
);

You can target the whitespace text nodes with XPath.

If the nodes you want to remove are //entity[name='mac'] then the whitespace text node before it would be

//text()[normalize-space(.) = ''][following-sibling::entity[position()=1][name='mac']]

And then, you can remove both nodesets by using the | operator and your command becomes:

$this->xmlDocument->removeNodes(
    "//entity[name='mac'] | //text()[normalize-space(.) = ''][following-sibling::entity[position()=1][name='mac']]"
);
情话已封尘 2024-09-02 14:22:40

如果您想删除输出中的所有这些空格,可以使用以下简单方法:

$this->xmlDocument->removeNodes(
    "//entity[name='mac'] | //text()[normalize-space(.) = '']"
);

Just in case you'd like to remove ALL these blank spaces for your output, there's a simple way below:

$this->xmlDocument->removeNodes(
    "//entity[name='mac'] | //text()[normalize-space(.) = '']"
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文