根据属性值复制 XML 文件的部分内容

发布于 2024-12-09 15:00:47 字数 3017 浏览 0 评论 0原文

我有一个很大的 xml 文件,需要创建该文件的部分“副本”。 (使用C#) 我需要保持相同的 xml 结构,但只需要条件为“true”的部分。

示例结构:

<?xml version="1.0" encoding="utf-8"?>
<country name="Netherlands">
    <region name="NH">
        <city name="Aalsmeer">
            <district name="Some district">
            <part type="slum" />
            </district>
            <contact adres="StreetName" telephone="0000000000" valid="false" />
            <residents number="10000" />
            <homes number = "7000" />
        </city>
        <city name="Heemskerk">
            <district name="Some district">
                <part type="workersDistrict" />
            </district>
            <contact adres="StreetName" telephone="0000000000" valid="true" />
            <residents number="10000" />
            <homes number = "7000" />
        </city>
        </region>
        <region name="ZH">
            <city name="Rotterdam">
                <district name="Some district">
                <part type="workersDistrict" />
                </district>
                <contact adres="StreetName" telephone="0000000000" valid="true" />
                <residents number="10000" />
                <homes number = "7000" />
            </city>
            <city name="Moerdijk">
                <district name="Some district">
                    <part type="residential area" />
                </district>
                <contact adres="StreetName" telephone="0000000000" valid="false" />
                <residents number="10000" />
                <homes number = "7000" />
            </city>
            </region>
</country>

我只需要属性“valid”为“true”的“city”元素。 新的 XML 文件应如下所示:

<?xml version="1.0" encoding="utf-8"?>
    <country name="Netherlands">
        <region name="NH">
            <city name="Heemskerk">
                        <district name="Some district"
                    <part type="workersDistrict" />
                </district>
                <contact adres="StreetName" telephone="0000000000" valid="true" />
                <residents number="10000" />
                <homes number = "7000" />
            </city>
        </region>
        <region name="ZH">
            <city name="Rotterdam">
                <district name="Some district"
                    <part type="workersDistrict" />
                </district>
                <contact adres="StreetName" telephone="0000000000" valid="true" />
                <residents number="10000" />
                <homes number = "7000" />
            </city>
        </region>
    </country>

如何尽快完成此操作(考虑到城市元素的数量(例如 100630)和文件大小(例如 63,0 MB)?

I've a large xml file and need to create a partial 'copy' of this file. (Using C#)
I need to keep the same xml structure, but only need the parts where a condition is 'true'.

An example structure:

<?xml version="1.0" encoding="utf-8"?>
<country name="Netherlands">
    <region name="NH">
        <city name="Aalsmeer">
            <district name="Some district">
            <part type="slum" />
            </district>
            <contact adres="StreetName" telephone="0000000000" valid="false" />
            <residents number="10000" />
            <homes number = "7000" />
        </city>
        <city name="Heemskerk">
            <district name="Some district">
                <part type="workersDistrict" />
            </district>
            <contact adres="StreetName" telephone="0000000000" valid="true" />
            <residents number="10000" />
            <homes number = "7000" />
        </city>
        </region>
        <region name="ZH">
            <city name="Rotterdam">
                <district name="Some district">
                <part type="workersDistrict" />
                </district>
                <contact adres="StreetName" telephone="0000000000" valid="true" />
                <residents number="10000" />
                <homes number = "7000" />
            </city>
            <city name="Moerdijk">
                <district name="Some district">
                    <part type="residential area" />
                </district>
                <contact adres="StreetName" telephone="0000000000" valid="false" />
                <residents number="10000" />
                <homes number = "7000" />
            </city>
            </region>
</country>

I only need the 'city' elements where the attribute 'valid' is 'true'.
The new XML file should look like this:

<?xml version="1.0" encoding="utf-8"?>
    <country name="Netherlands">
        <region name="NH">
            <city name="Heemskerk">
                        <district name="Some district"
                    <part type="workersDistrict" />
                </district>
                <contact adres="StreetName" telephone="0000000000" valid="true" />
                <residents number="10000" />
                <homes number = "7000" />
            </city>
        </region>
        <region name="ZH">
            <city name="Rotterdam">
                <district name="Some district"
                    <part type="workersDistrict" />
                </district>
                <contact adres="StreetName" telephone="0000000000" valid="true" />
                <residents number="10000" />
                <homes number = "7000" />
            </city>
        </region>
    </country>

How do I get this done as quickly as possible (taking into account the number of city elements (e.g. 100630) and the file size (e.g. 63,0 MB)?

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

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

发布评论

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

评论(2

天暗了我发光 2024-12-16 15:00:47

使用修改后的身份转换,您可以简单地匹配要抑制的元素并提供一个空模板。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output indent="yes" />

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!--suppress any city elements that do not have contact/@valid='true' -->
    <xsl:template match="city[not(contact/@valid='true')]" />

</xsl:stylesheet>

您可以像这样在 C# 中执行 XSLT

using System;
using System.Xml;
using System.Xml.Xsl; 
namespace XSLTransformation
{
    /// Summary description for Class1.
    class Class1
    {
        static void Main(string[] args)
        {
            XslTransform myXslTransform; 
            myXslTransform = new XslTransform();
            myXslTransform.Load("books.xsl"); 
            myXslTransform.Transform("books.xml", "ISBNBookList.xml"); 

        }
    }
}

Using a modified identity transform, you can simply match on the elements that you want to suppress and provide an empty template.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output indent="yes" />

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!--suppress any city elements that do not have contact/@valid='true' -->
    <xsl:template match="city[not(contact/@valid='true')]" />

</xsl:stylesheet>

You can execute the XSLT in C# like this:

using System;
using System.Xml;
using System.Xml.Xsl; 
namespace XSLTransformation
{
    /// Summary description for Class1.
    class Class1
    {
        static void Main(string[] args)
        {
            XslTransform myXslTransform; 
            myXslTransform = new XslTransform();
            myXslTransform.Load("books.xsl"); 
            myXslTransform.Transform("books.xml", "ISBNBookList.xml"); 

        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文