我有一個定義簡單類型和複雜類型的.xsd模式 - 下面是一個示例。我需要能夠加載這個.xsd文件,添加更多元素並將其作爲字符串返回(最終將通過api發送給第三方服務)。該網站建立在Drupal上,因此代碼需要使用PHP。在PHP中動態創建和更新.xsd模式?
下面是模式(.xsd)的樣品
是否有任何工具或庫,這將使更容易嗎?在SimpleXML中,我在php中看到了許多xml實用程序,但在我看到的示例中,他們已經具有.xsd文件,並正在根據給定架構解析和創建對象並在xml文檔中設置值。我想通過編程方式更改和更新架構.xsd文件?任何提示?或圖書館或appraoches將不勝感激。我即將開始以不太理想的方式來編寫代碼(但它會起作用),它每次只重建整個.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>
我的代碼的當前版本使用了很多像這樣的字符串
$newschemaObject = MakeDynmaicObject(oldschema.xsd);
$newschema->add(simple, text, "New Text Field", required);
$stringtosend = $newschema->buildXSD();
schema.xsd樣本:
$xsd .= '<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">';
$xsd .= ' <xsd:element name="metadata">';
產生上述架構,但它不靈活或非常編程。任何關於能夠更新the.xsd並以面向對象的方式設置新元素的建議將非常棒!
可能重複(http://stackoverflow.com/questions/3244563/parse-xml-using-a-xsd-in-php) –
你的問題不明確。你是否要求使用基於字符串而不是文件名的驗證方法(是的,這是可能的),還是在詢問如何修改XML文檔(是的,這也是可能的)。在目前的形式下,你的問題並不清楚。像「*有任何工具或庫會變得更容易嗎?」的句子無論如何都是紅旗。 – hakre
對不起,我的問題並不清楚,我原本不知道要正確說出它,但我需要做的是使用PHP的SimpleXML(您也可以使用XMLWriter)以允許動態的方式從頭創建一個.xsd文件添加字段。我會回答我自己的問題,並顯示一個代碼示例,再次抱歉不清楚 – user1073319