【问题标题】:Remove all XML Attributes from Atom XML从 Atom XML 中删除所有 XML 属性
【发布时间】:2016-02-18 17:27:38
【问题描述】:

我们正在尝试从 XML 中删除所有属性。我们使用的解析器无法处理 XML 中提到的属性。

我们认为不可能在一个正则表达式中实现,因此我们正在寻找替代方案。 我们认为最好的方法可能是为每个标签创建一个正则表达式。

示例 XML:

<?xml version='1.0' encoding='utf-8'?>
<feed xml:base="https://hcm12preview.sapsf.eu/odata/v2/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <title type="text">Todo</title>
    <id>https://hcm12preview.sapsf.eu/odata/v2/Todo</id>
    <updated>2015-11-17T10:30:34Z</updated>
    <link href="Todo" rel="self" title="Todo"/>
    <entry>
        <id>https://hcm12preview.sapsf.eu/odata/v2/Todo('5')</id>
        <title type="text"/>
        <updated>2015-11-17T10:30:34Z</updated>
        <author>
            <name/>
        </author>
        <link href="Todo('5')" rel="edit" title="Todo"/>
        <category scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" term="SFOData.Todo"/>
        <content type="application/xml">
            <m:properties>
                <d:categoryId>5</d:categoryId>
                <d:todos m:type="Bag(SFOData.ToDoBean)">
                    <d:element>
                        <d:categoryId>5</d:categoryId>
                        <d:completedDate m:null="true"/>
                        <d:dueDate m:null="true"/>
                        <d:dueDateOffSet m:type="Edm.Int32">-2147483648</d:dueDateOffSet>
                        <d:entries m:type="Bag(SFOData.ToDoEntry)">
                            <d:element>
                                <d:completedDate m:null="true"/>
                                <d:formDataId m:type="Edm.Int64">15301</d:formDataId>
                                <d:status m:type="Edm.Int32">2</d:status>
                                <d:statusLabel>Actief</d:statusLabel>
                                <d:subjectFullName>Recruitment Request :1508 - Receptionist</d:subjectFullName>
                                <d:subjectId m:null="true"/>
                                <d:url>https://www.example.com</d:url>
                            </d:element>
                        </d:entries>
                        <d:entryId m:type="Edm.Int32">10884</d:entryId>
                        <d:name>Ontwerp aanvraag</d:name>
                        <d:status m:type="Edm.Int32">2</d:status>
                        <d:statusLabel>Actief</d:statusLabel>
                        <d:stepDescAlt>Ontwerp aanvraag</d:stepDescAlt>
                        <d:todoItemId m:null="true"/>
                        <d:url>https://www.example.com</d:url>
                    </d:element>
                    <d:element>
                        <d:categoryId>5</d:categoryId>
                        <d:completedDate m:null="true"/>
                        <d:dueDate m:null="true"/>
                        <d:dueDateOffSet m:type="Edm.Int32">-2147483648</d:dueDateOffSet>
                        <d:entries m:type="Bag(SFOData.ToDoEntry)">
                            <d:element>
                                <d:completedDate m:null="true"/>
                                <d:formDataId m:type="Edm.Int64">15304</d:formDataId>
                                <d:status m:type="Edm.Int32">2</d:status>
                                <d:statusLabel>Actief</d:statusLabel>
                                <d:subjectFullName>Recruitment Request : 9002 - Facility &amp; Fleet Coordinator</d:subjectFullName>
                                <d:subjectId m:null="true"/>
                                <d:url>https://www.example.com</d:url>
                            </d:element>
                            <d:element>
                                <d:completedDate m:null="true"/>
                                <d:formDataId m:type="Edm.Int64">15302</d:formDataId>
                                <d:status m:type="Edm.Int32">1</d:status>
                                <d:statusLabel>ophanden</d:statusLabel>
                                <d:subjectFullName>Recruitment Request : 2001 - Chief Business Innovation Officer</d:subjectFullName>
                                <d:subjectId m:null="true"/>
                                <d:url>https://www.example.com</d:url>
                            </d:element>
                            <d:element>
                                <d:completedDate m:null="true"/>
                                <d:formDataId m:type="Edm.Int64">15303</d:formDataId>
                                <d:status m:type="Edm.Int32">1</d:status>
                                <d:statusLabel>ophanden</d:statusLabel>
                                <d:subjectFullName>Recruitment Request : 2001 - Chief Business Innovation Officer</d:subjectFullName>
                                <d:subjectId m:null="true"/>
                                <d:url>https://www.example.com</d:url>
                            </d:element>
                        </d:entries>
                        <d:entryId m:type="Edm.Int32">10905</d:entryId>
                        <d:name>Aanvraag bij Board</d:name>
                        <d:status m:type="Edm.Int32">2</d:status>
                        <d:statusLabel>Actief</d:statusLabel>
                        <d:stepDescAlt>Aanvraag bij Board</d:stepDescAlt>
                        <d:todoItemId m:null="true"/>
                        <d:url>https://www.example.com</d:url>
                    </d:element>
                </d:todos>
                <d:categoryLabel>Nieuw talent rekruteren</d:categoryLabel>
                <d:displayOrder m:type="Edm.Int32">5</d:displayOrder>
            </m:properties>
        </content>
    </entry>
    <entry>
        <id>https://hcm12preview.sapsf.eu/odata/v2/Todo('11')</id>
        <title type="text"/>
        <updated>2015-11-17T10:30:34Z</updated>
        <author>
            <name/>
        </author>
        <link href="Todo('11')" rel="edit" title="Todo"/>
        <category scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" term="SFOData.Todo"/>
        <content type="application/xml">
            <m:properties>
                <d:categoryId>11</d:categoryId>
                <d:todos m:type="Bag(SFOData.ToDoBean)">
                    <d:element>
                        <d:categoryId>11</d:categoryId>
                        <d:completedDate m:null="true"/>
                        <d:dueDate m:type="Edm.DateTime">2015-08-28T00:00:00</d:dueDate>
                        <d:dueDateOffSet m:type="Edm.Int32">81</d:dueDateOffSet>
                        <d:entries m:type="Bag(SFOData.ToDoEntry)">
                            <d:element>
                                <d:completedDate m:null="true"/>
                                <d:formDataId m:type="Edm.Int64">14352</d:formDataId>
                                <d:status m:type="Edm.Int32">2</d:status>
                                <d:statusLabel>Actief</d:statusLabel>
                                <d:subjectFullName>Test Tester for 8050 - Technieker int Version - 3</d:subjectFullName>
                                <d:subjectId m:null="true"/>
                                <d:url>https://hcm12preview.sapsf.eu/sf/recruiting/jobreqsummary/offerapprovals?company=DummyCompany</d:url>
                            </d:element>
                            <d:element>
                                <d:completedDate m:null="true"/>
                                <d:formDataId m:type="Edm.Int64">14101</d:formDataId>
                                <d:status m:type="Edm.Int32">2</d:status>
                                <d:statusLabel>Actief</d:statusLabel>
                                <d:subjectFullName>Guy Londers for 8050 - Technieker int Version - 1</d:subjectFullName>
                                <d:subjectId m:null="true"/>
                                <d:url>https://hcm12preview.sapsf.eu/sf/recruiting/jobreqsummary/offerapprovals?company=DummyCompany</d:url>
                            </d:element>
                            <d:element>
                                <d:completedDate m:null="true"/>
                                <d:formDataId m:type="Edm.Int64">14251</d:formDataId>
                                <d:status m:type="Edm.Int32">2</d:status>
                                <d:statusLabel>Actief</d:statusLabel>
                                <d:subjectFullName>svea huygelen for 8050 - Technieker int Version - 1</d:subjectFullName>
                                <d:subjectId m:null="true"/>
                                <d:url>https://hcm12preview.sapsf.eu/sf/recruiting/jobreqsummary/offerapprovals?company=DummyCompany</d:url>
                            </d:element>
                        </d:entries>
                        <d:entryId m:type="Edm.Int32">10004</d:entryId>
                        <d:name>Goedkeuringen aanbiedingen in behandeling</d:name>
                        <d:status m:type="Edm.Int32">2</d:status>
                        <d:statusLabel>Actief</d:statusLabel>
                        <d:stepDescAlt>Goedkeuringen aanbiedingen in behandeling</d:stepDescAlt>
                        <d:todoItemId m:null="true"/>
                        <d:url>https://hcm12preview.sapsf.eu/sf/recruiting/jobreqsummary/offerapprovals?company=DummyCompany</d:url>
                    </d:element>
                    <d:element>
                        <d:categoryId>11</d:categoryId>
                        <d:completedDate m:null="true"/>
                        <d:dueDate m:type="Edm.DateTime">2015-10-04T00:00:00</d:dueDate>
                        <d:dueDateOffSet m:type="Edm.Int32">44</d:dueDateOffSet>
                        <d:entries m:type="Bag(SFOData.ToDoEntry)">
                            <d:element>
                                <d:completedDate m:null="true"/>
                                <d:formDataId m:type="Edm.Int64">14552</d:formDataId>
                                <d:status m:type="Edm.Int32">2</d:status>
                                <d:statusLabel>Actief</d:statusLabel>
                                <d:subjectFullName>Tom Verplancken voor 8050 - Technieker int Versie - 2</d:subjectFullName>
                                <d:subjectId m:null="true"/>
                                <d:url>https://hcm12preview.sapsf.eu/sf/recruiting/jobreqsummary/offerapprovals?company=DummyCompany</d:url>
                            </d:element>
                            <d:element>
                                <d:completedDate m:null="true"/>
                                <d:formDataId m:type="Edm.Int64">14551</d:formDataId>
                                <d:status m:type="Edm.Int32">2</d:status>
                                <d:statusLabel>Actief</d:statusLabel>
                                <d:subjectFullName>Nadine Martens voor 8050 - Technieker int Versie - 2</d:subjectFullName>
                                <d:subjectId m:null="true"/>
                                <d:url>https://hcm12preview.sapsf.eu/sf/recruiting/jobreqsummary/offerapprovals?company=DummyCompany</d:url>
                            </d:element>
                        </d:entries>
                        <d:entryId m:type="Edm.Int32">10224</d:entryId>
                        <d:name>Goedkeuringen aanbiedingen in behandeling</d:name>
                        <d:status m:type="Edm.Int32">2</d:status>
                        <d:statusLabel>Actief</d:statusLabel>
                        <d:stepDescAlt>Goedkeuringen aanbiedingen in behandeling</d:stepDescAlt>
                        <d:todoItemId m:null="true"/>
                        <d:url>https://hcm12preview.sapsf.eu/sf/recruiting/jobreqsummary/offerapprovals?company=DummyCompany</d:url>
                    </d:element>
                    <d:element>
                        <d:categoryId>11</d:categoryId>
                        <d:completedDate m:null="true"/>
                        <d:dueDate m:type="Edm.DateTime">2015-10-15T00:00:00</d:dueDate>
                        <d:dueDateOffSet m:type="Edm.Int32">33</d:dueDateOffSet>
                        <d:entries m:type="Bag(SFOData.ToDoEntry)">
                            <d:element>
                                <d:completedDate m:null="true"/>
                                <d:formDataId m:type="Edm.Int64">14852</d:formDataId>
                                <d:status m:type="Edm.Int32">2</d:status>
                                <d:statusLabel>Actief</d:statusLabel>
                                <d:subjectFullName>Brenda Peters for 9007 - Technical Facility Teamleader Version - 3</d:subjectFullName>
                                <d:subjectId m:null="true"/>
                                <d:url>https://hcm12preview.sapsf.eu/sf/recruiting/jobreqsummary/offerapprovals?company=DummyCompany</d:url>
                            </d:element>
                        </d:entries>
                        <d:entryId m:type="Edm.Int32">10369</d:entryId>
                        <d:name>Goedkeuringen aanbiedingen in behandeling</d:name>
                        <d:status m:type="Edm.Int32">2</d:status>
                        <d:statusLabel>Actief</d:statusLabel>
                        <d:stepDescAlt>Goedkeuringen aanbiedingen in behandeling</d:stepDescAlt>
                        <d:todoItemId m:null="true"/>
                        <d:url>https://hcm12preview.sapsf.eu/sf/recruiting/jobreqsummary/offerapprovals?company=DummyCompany</d:url>
                    </d:element>
                </d:todos>
                <d:categoryLabel>Goedkeuring van aanbieding</d:categoryLabel>
                <d:displayOrder m:type="Edm.Int32">11</d:displayOrder>
            </m:properties>
        </content>
    </entry>
</feed>

【问题讨论】:

  • 为什么需要正则表达式?
  • 我们在 ABAP 中这样做,ABAP 是一种来自 SAP 的编程语言。它无法处理 XML 中的属性。创建此 XML 的软件是 SuccessFactors,您无法更改有关输出的任何内容。
  • 您使用的是什么操作系统?在 C# 中编写一个实用程序来执行此操作可能更容易。 What's the easiest way to remove all attributes from a XML in C#? 另外,您可能想让 SuccessFactors 知道他们的软件产生的输出与 ABAP 不兼容,因为他们是一家 SAP 公司。
  • 就像他们会为 1 个用户更改整个流程 :)
  • 你肯定不想为此使用正则表达式,你想使用像 XSLT 这样的 XML 感知工具。转换应该在什么平台上运行?

标签: regex xml regex-negation regex-lookarounds atom-feed


【解决方案1】:

嗯...也许这对你有帮助。

\<[a-zA-Z0-9\s\=\":\.]+\>(.*)<\/[a-zA-Z\:]+>

https://regex101.com/r/jL3bT7/1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2012-05-31
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多