作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
PHP - 如果元素每条记录具有相似的标签,则获取正确的 XML 值
我正在获取以下 xml 文件:
XML 文件:
<?xml version='1.0' encoding='UTF-8'?>
<abc:ABCData xmlns:abc="http://www.abc-example.com" xmlns:xyz="http:/www.xyz-example.com">
<abc:ABCRecords>
<abc:ABCRecord>
<abc:ABC>5EXZX4LPK</abc:ABC>
<abc:Entity>
<abc:Name>I Bornheim</abc:Name>
<abc:Periods>
<abc:Period>
<abc:Start>2017-01-01</abc:Start>
<abc:End>2017-12-31</abc:End>
<abc:Type>ACCOUNTING</abc:Type>
</abc:Period>
<abc:Period>
<abc:Start>2007-09-01</abc:Start>
<abc:Type>RELATIONSHIP</abc:Type>
</abc:Period>
</abc:Periods>
</abc:Entity>
</abc:ABCRecord>
<abc:ABCRecord>
<abc:ABC>5967007LI</abc:ABC>
<abc:Entity>
<abc:Name>SUN BANK</abc:Name>
<abc:Periods>
<abc:Period>
<abc:Start>2018-01-01</abc:Start>
<abc:End>2018-12-31</abc:End>
<abc:Type>BALANCED</abc:Type>
</abc:Period>
<abc:Period>
<abc:Start>2008-09-01</abc:Start>
<abc:Type>PARENT</abc:Type>
</abc:Period>
</abc:Periods>
</abc:Entity>
</abc:ABCRecord>
</abc:ABCRecords>
</abc:ABCData>
<?php
$reader = new XMLReader();
$reader->open('php://stdin');
$output = fopen('php://stdout', 'w');
fputcsv($output, ['id', 'name']);
$xmlns = [
'abc' => 'http://www.abc-example.com'
];
$dom = new DOMDocument;
$xpath = new DOMXpath($dom);
foreach ($xmlns as $prefix => $namespaceURI) {
$xpath->registerNamespace($prefix, $namespaceURI);
}
while (
$reader->read() &&
(
$reader->localName !== 'ABCRecord' ||
$reader->namespaceURI !== $xmlns['abc']
)
) {
continue;
}
while ($reader->localName === 'ABCRecord') {
if ($reader->namespaceURI === 'http://www.abc-example.com') {
$node = $reader->expand($dom);
fputcsv(
$output,
[
$xpath->evaluate('string(abc:ABC)', $node),
$xpath->evaluate('string(abc:Entity/abc:Name)', $node)
]
);
}
$reader->next('ABCRecord');
}
5EXZX4LPK,"I Bornheim"
5967007LI,"SUN BANK"
5EXZX4LPK,"I Bornheim",2017-01-01,2017-12-31,"ACCOUNTING",2007-09-01,"RELATIONSHIP"
5967007LI,"SUN BANK",2018-01-01,2018-12-31,"BALANCED",2008-09-01,"PARENT"
最佳答案
您可以使用该类型对期间进行分组并获取第一个分组结果:
$xpath->evaluate(
'string(
abc:Entity/abc:Periods/abc:Period[
abc:Type = "ACCOUNTING" or abc:Type="BALANCED"
][1]/abc:Type
)',
$node
),
abc:End
子元素:
$xpath->evaluate(
'string(
abc:Entity/abc:Periods/abc:Period[
count(abc:End) = 0
][1]/abc:Type
)',
$node
),
关于PHP - 如果元素每条记录具有相似的标签,则获取正确的 XML 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53797403/
我刚开始使用 Gnu Plot 并创建了一些简单的绘图。但是现在我遇到了一个新问题。 输入是这样的 csv 文件: name;n0;n1;n2 Benj;1;3;2 Silv;6;1;2 Steffi
我在 MongoDB 中有 2700 条记录。每个文档的大小约为 320KB。我使用的引擎是wiredTiger,集合的总大小约为885MB。 我的 MongoDB 配置如下: systemLog:
我是一名优秀的程序员,十分优秀!