2013-04-12 24 views
1

我嘗試擴展sitemap.xsd:我想向tUrl complexType添加一個新元素(稱爲'crawl')。擴展或重新定義XSD complexType

所以我創建了一個sitemap-extended.xsd(我已經重新定義了sitemap.xsd並且我已經擴展了tUrl)。 :

<?xml version="1.0" encoding="UTF-8"?> 
<xsd:schema 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://www.sitemaps.org/schemas/sitemap/0.9" 
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 
elementFormDefault="qualified"> 

<xsd:redefine schemaLocation="sitemap.xsd"> 
    <xsd:complexType name="tUrl"> 
     <xsd:complexContent mixed="false"> 
      <xsd:extension base="tUrl"> 
       <xsd:sequence> 
        <xsd:element name="crawl" type="xsd:string" maxOccurs="1" /> 
       </xsd:sequence> 
      </xsd:extension> 
     </xsd:complexContent> 
    </xsd:complexType> 
</xsd:redefine> 
</xsd:schema> 

這是工作,但生成的XML是無效的(例如Google不會有效這個XML由於未知的實體 - 爬行)。所以我認爲我應該使用不同的命名空間來實現這一點,但我沒有找到任何解決方案。

我希望能夠馬歇爾/和解組這種XML的:

<?xml version='1.0' encoding='UTF-8'?> 
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 
xmlns:ext="http://www.mycompany.com/schema/myns" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 
    http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd 
    http://www.mycompany.com/schema/myns http://www.mycompany.com/schema/myns/sitemap.xsd"> 
    <url> 
    <loc>...</loc> 
    <ext:crawl>...</ext:crawl> 
     ... 
    </url> 
    </urlset> 

任何想法?謝謝

回答

1

這裏有幾個問題。

你修改XSD描述不能被sitemap.xsd因爲你從http://www.sitemaps.org/schemas/sitemap/0.9添加內容進行驗證個XML - 這是不允許的。

enter image description here

正確的方法是將crawl到另一個名稱空間,然後引用它。

擴展XSD:

<?xml version="1.0" encoding="utf-8" ?> 
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) --> 
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" xmlns="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="crawl" type="xsd:string"/> 
</xsd:schema> 

修改重新定義:

<?xml version="1.0" encoding="utf-8"?> 
<!--XML Schema generated by QTAssistant/XSR Module (http://www.paschidev.com)--> 
<xsd:schema xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:ext="http://tempuri.org/XMLSchema.xsd" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:redefine schemaLocation="../Standards/sitemap/sitemap.xsd"> 
    <xsd:complexType name="tUrl"> 
     <xsd:complexContent> 
     <xsd:restriction base="tUrl"> 
      <xsd:sequence> 
      <xsd:element name="loc" type="tLoc"/> 
      <xsd:element name="lastmod" type="tLastmod" minOccurs="0"/> 
      <xsd:element name="changefreq" type="tChangeFreq" minOccurs="0"/> 
      <xsd:element name="priority" type="tPriority" minOccurs="0"/> 
      <xsd:element ref="ext:crawl"/> 
     </xsd:sequence> 
     </xsd:restriction> 
     </xsd:complexContent> 
    </xsd:complexType> 
    </xsd:redefine> 
    <xsd:import namespace="http://tempuri.org/XMLSchema.xsd" schemaLocation="extending-or-redefining-xsd-complextype-ext.xsd"/> 
</xsd:schema> 

現在,儘管這在技術上是正確的,這不加載Extension XSD實體應該失敗含有ext:crawl個XML的驗證只是因爲sitemap.xsd使用元素通配符processContents="strict"

我還沒有嘗試過,但您可以通過xsi:schemaLocation提供您的擴展XSD的URL(我會驚訝地發現Google的驗證器遵循外部架構位置提示)。

如果您打算將修改後的XSD與JAXB一起使用,並希望現在可以在您的類定義中看到一個用於抓取的字段,您可能會感到驚訝(上次我檢查了,通過限制重新定義,至少沒有工作)。

您仍可以使用ext:crawl取消/編組XML,您只需使用XmlNode手動執行。

+0

感謝Petru爲這個完美的答案。 – willome