为了账号安全,请及时绑定邮箱和手机立即绑定

在 xml php 中添加前缀字符串所有标签

在 xml php 中添加前缀字符串所有标签

PHP
aluckdog 2023-08-19 17:55:36
你好,我尝试在我的 xml 标签上添加前缀,如下所示 <identifiant>       <a:Nom>NOM</a:Nom>       <a:NomJeuneFille i:nil="true" />       <a:Prenom>PRENOM</a:Prenom>       </identifiant>谁能帮我
查看完整描述

2 回答

?
慕斯王

TA贡献1864条经验 获得超2个赞

这是一个 XML 命名空间前缀,如果没有匹配的 XML 命名空间定义,它是无效的。DOM 有特殊的方法来创建带有命名空间的 XML 节点:


// a list of namespaces just to avoid repeating the URIs

// the keys do not have to match the prefixes in the XML

$namespaces = [

  'xmlns' => 'http://www.w3.org/2000/xmlns/',

  'a' => 'urn:example:a',

  'i' => 'urn:example:i'

];


$document = new DOMDocument();

// the document element does not seem to have a namespace

$identifiant = $document->appendChild(

    $document->createElement('identifiant')

);

// add the namespace definition for prefix "a"

// namespace definition use a reserved, internal namespace

$identifiant->setAttributeNS($namespaces['xmlns'], 'xmlns:a', $namespaces['a']);


$identifiant->appendChild(

    // create an elment node with the namespace

    $document->createElementNS(

        $namespaces['a'],

        'a:Nom'

    )

)->textContent = 'NOM';


$identifiant

  ->appendChild(

    $document->createElementNS(

        $namespaces['a'],

        'a:NomJeuneFille'

    )

  )

  ->setAttributeNS(

        $namespaces['i'],

        'i:nil',

        'true'

  );



$identifiant->appendChild(

    $document->createElementNS(

        $namespaces['a'],

        'a:Prenom'

    )

)->textContent = 'PRENOM';


$document->formatOutput = TRUE;

echo $document->saveXML();

输出:


<?xml version="1.0"?>

<identifiant xmlns:a="urn:example:a">

  <a:Nom>NOM</a:Nom>

  <a:NomJeuneFille xmlns:i="urn:example:i" i:nil="true"/>

  <a:Prenom>PRENOM</a:Prenom>

</identifiant>

将根据需要添加命名空间定义。您可以通过在祖先节点上手动定义它来避免在兄弟分支上重复它。我对“a”前缀执行此操作,“i”前缀的定义是自动添加的。


查看完整回答
反对 回复 2023-08-19
?
MYYA

TA贡献1868条经验 获得超4个赞

尝试对所选内容进行查找和替换(大多数文本编辑器允许您执行此操作)

如果您的文件看起来相似,您可以通过滥用以空格开头的想法来以最小的努力替换开始块

  • 寻找      <

  • 代替      <a:


如果这还不够(即您有一个更复杂的文件或希望对大量文件执行此操作),您将需要使用 XML 解析器重排您的 XML 文档

一个程序看起来像

import XML parsing library

read XML file

iterate over the blocks of whatever depth until you find what you want

change the block names (perhaps copy to new blocks and switch 'em)

write the resulting file again (consider writing to a new file so you can compare them more easily)



查看完整回答
反对 回复 2023-08-19
  • 2 回答
  • 0 关注
  • 98 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信