1

I have a .xsd schema that defines both simple and complex types - a sample is below. I need to be able to load this .xsd file, add more elements to it and return it as a string (which will eventually be sent via an api to a 3rd party service). The website is built on Drupal, so the code needs to be in PHP.

Below is a sample of the schema (.xsd)

Are there any tools or libraries that will make easier? I see lots of xml utilities in php like SimpleXML, but in the examples I looked at they already have the .xsd file and are parsing and creating objects and setting values in an xml document based on a given schema. I want to programatically change and update a schema .xsd file via php?Any tips? or libraries or appraoches would be greatly appreciated. I'm about to start wrtiing the code in a less than ideal way (but it will work), which just rebuilds the entire .xsd each time and has multiple loops using strings to add new elements, but that's not the best solution. I'm hoping there is a tool that may work something like this:

$newschemaObject = MakeDynmaicObject(oldschema.xsd);
$newschema->add(simple, text, "New Text Field", required);
$stringtosend = $newschema->buildXSD();

Sample of schema.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <xsd:element name="metadata">
    <xsd:complexType>
  <xsd:sequence>
    <xsd:element name="DrupalEntityId" minOccurs="0" maxOccurs="1" type="textType">
      <xsd:annotation>
        <xsd:documentation/>
        <xsd:appinfo>
          <label>Drupal Entity ID</label>
          <key>Drupal Entity ID</key>
          <searchable>true</searchable>
          <timeControl>false</timeControl>
          <description/>
        </xsd:appinfo>
      </xsd:annotation>
    </xsd:element>
    <xsd:element id="md_631560E7-14AD-5327-732B-8268813A0553" name="SampleMultiValueTextField" minOccurs="0" maxOccurs="unbounded" type="textType">
      <xsd:annotation>
        <xsd:documentation/>
        <xsd:appinfo>
          <label>Sample Multi Value Text Field</label>
          <key>Sample Multi Value Text Field</key>
          <searchable>true</searchable>
          <timeControl>false</timeControl>
          <description>This is a sample multivalue text field</description>
        </xsd:appinfo>
      </xsd:annotation>
    </xsd:element>
 ...
</xsd:schema>

The current version of my code uses a lot of strings like this:

$xsd .= '<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">';
$xsd .= '  <xsd:element name="metadata">';

to generate the above schema, but it's not flexible or very programmatic. any advice on being able to update the.xsd and set new elements in a object oriented way would be amazing!!!!

4

1 回答 1

2

我最初的问题不够清楚,所以我会自己澄清并回答。我最初在 PHP 中输​​出一个硬编码字符串以用作 xml .xsd 模式,它不允许动态更改定义以添加新元素或属性。我最终使用 SimpleXML 创建了我的 .xsd 字符串(也可以保存为文件)。

$xmlSchema = new SimpleXMLElement('<xmlns:xsd:schema></xmlns:xsd:schema>', LIBXML_NOERROR, false, 'xsd', true);
$xmlSchema->addAttribute('xmlns:xmlns:xsd', 'http://www.w3.org/2001/XMLSchema');
$newelement = $xmlSchema->addChild('xsd:xsd:element');
$newelement->addAttribute('name', 'metadata');
$newelement = $metaelement->addChild($elmprefix.'complexType');
$sequence = $newelement->addChild($elmprefix.'sequence');
于 2013-07-01T17:39:24.180 回答