2016-08-12 4 views
1

私はそのXSDスキーマを取得し、Pythonのlxmlのライブラリを使用して検証することにより、生成するサイトマップXMLを検証するユニットテストを書いている:lxmlを使用して複数のxsdスキーマに対して検証するにはどうすればよいですか?

は、ここに私のルート要素にいくつかのメタデータです:

xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" 
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd 
http://www.google.com/schemas/sitemap-image/1.1 
http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd" 

そして、このテストコード:検証に失敗した

_xsd_validators = {} 
def get_xsd_validator(url): 
    if url not in _xsd_validators: 
     _xsd_validators[url] = etree.XMLSchema(etree.parse(StringIO(requests.get(url).content))) 
    return _xsd_validators[url] 


# this util function is later on in a TestCase 
def validate_xml(self, content): 
    content.seek(0) 
    doc = etree.parse(content) 
    schema_loc = doc.getroot().attrib.get('{http://www.w3.org/2001/XMLSchema-instance}schemaLocation').split(' ') 
    # lxml doesn't like multiple namespaces 
    for i, loc in enumerate(schema_loc): 
     if i % 2 == 1: 
      get_xsd_validator(schema_loc[i]).assertValid(doc) 
    return doc 

例のXML:

<?xml version="1.0" encoding="UTF-8"?> 
<urlset 
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 
    xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" 
    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.google.com/schemas/sitemap-image/1.1 
    http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd" 
> 
    <url> 
    <loc>https://www.example.com/press</loc> 
    <lastmod>2016-08-11</lastmod> 

    <changefreq>weekly</changefreq> 
    </url> 

    <url> 
    <loc>https://www.example.com/about-faq</loc> 
    <lastmod>2016-08-11</lastmod> 

    <changefreq>weekly</changefreq> 
    </url> 


</urlset> 
私は、通常のサイトマップのすべてが素晴らしい仕事を持っていたが、私は画像のサイト​​マップのマークアップ assertValidに追加したとき

がで失敗しました:

E DocumentInvalid: Element '{http://www.google.com/schemas/sitemap-image/1.1}image': No matching global element declaration available, but demanded by the strict wildcard., line 12 

または:

E DocumentInvalid: Element '{http://www.sitemaps.org/schemas/sitemap/0.9}urlset': No matching global declaration available for the validation root., line 6 

答えて

4

をあなたはラッパーのスキーマを定義しようとすることができwrapper-schema.xsdは、必要なすべてのスキーマをインポートし、このスキーマを他のスキーマの代わりにlxmlで使用します。私のpythonを持っていないが、これは酸素で正常に検証

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:import 
    namespace="http://www.sitemaps.org/schemas/sitemap/0.9" 
    schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"/> 
    <xs:import 
    namespace="http://www.google.com/schemas/sitemap-image/1.1" 
    schemaLocation="http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd"/> 
</xs:schema> 

<?xml version="1.0" encoding="UTF-8"?> 
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 
    xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="wrapper-schema.xsd" 
    > 
    <image:image> 
     <image:loc>http://www.example.com/image</image:loc> 
    </image:image> 
    <url> 
     <loc>https://www.example.com/press</loc> 
     <lastmod>2016-08-11</lastmod> 
     <changefreq>weekly</changefreq> 
    </url> 
    <url> 
     <loc>https://www.example.com/about-faq</loc> 
     <lastmod>2016-08-11</lastmod> 
     <changefreq>weekly</changefreq> 
    </url> 
</urlset> 
+0

おかげで、私はそれを試してみましょう。 –

+0

それは働いて、ありがとう! –

関連する問題