在 XML 文档中嵌入 XHTML 片段,并根据模式进行验证 [英] Emded XHTML snippet in XML document, and validate against a schema

查看:28
本文介绍了在 XML 文档中嵌入 XHTML 片段,并根据模式进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本质上,我想在一个 XML 文档中嵌入一些 XHTML,该文档必须根据自定义模式进行验证.

Essentially I want to embed some XHTML in an XML document that must validate against a custom schema.

完整背景:

我有一个使用 XML 文档的网络服务.此 XML 文档根据自定义模式进行验证.XML 中的数据被解析并存储在数据库中,并以有用的格式显示在网站上.

I have a webservice that consumes an XML document. This XML document is validated against a custom schema. The data in the XML is parsed and stored in a database, and displayed in a useful format on a website.

在我的网络服务上触发 XML 的客户有自己的内部IT/程序员".他希望能够在某些占位符中显示某些自定义 XHMTL网站页面.

The customer who fires the XML at my webservice has its own internal "IT / programmer guy". He wants to be able to display some custom XHMTL in some placeholders on some of the websites pages.

我们同意他可以扩展他在我的网络服务中触发的 XML,以包含 3 个将包含 HTML 的新元素,我将相应地调整我的架构.我还将进行处理,将他的 XHTML 从 XML 文档中提取到网页中.

We have agreed that he can extend the XML that he fires at my webservice to include 3 new elements that will contain the HTML, and I will adjust my schema accordingly. I'll also do the processing to get his XHTML out of the XML doc an on to the web pages.

我不想使用 cdata 因为这可能非常不安全(我认为!),所以我试图在架构中使用 :

I don't want to use cdata as that could be quite insecure (I think!), so I was trying to use an <xs:any> in the schema:

<xs:element name="SomeXhtmlStuff" minOccurs="0">
  <xs:complexType>
    <xs:sequence>
      <xs:any minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

我认为这意味着元素中的任何有效 XML 都可以,例如所有 XHTML 标签都可以,但是我试过了:

I was thinking this would mean that any valid XML would be OK in the element, e.g. all XHTML tags would be fine, however I tried this:

<SomeXhtmlStuff>
  <p>This is a test HTML output for Job Details</p>
</SomeXhtmlStuff>

并且 XML 不会对其进行验证.Visual Studio 2008 在它的自动验证器中给出错误'p' 元素未声明"

and the XML won't validate against it. Visual Studio 2008 in it automatic validator gives the error "the 'p' element is not declared"

我对 XML/模式没有太多经验,我继承了这个项目,非常欢迎任何建议!

I haven't got much experience with XML/schema and I inherited this project, any suggestions would be more than welcome!

提前致谢!

推荐答案

您可能应该考虑将 xhtml 命名空间放入 xs:any 元素中.您可能还想将 processContents 属性更改为lax".lax 属性值通知验证器,如果它可以找到定义,它应该验证内容.因此,更好的元素模型可能是:

You should probably consider putting the xhtml namespace into that xs:any element. You might also want to change the processContents attribute to 'lax'. The lax attribute value informs the validator that it should validate the content if it can locate a definition. So, a better element model might be:

<xs:element name="SomeXhtmlStuff" minOccurs="0" >
    <xs:complexType>
    <xs:complexContent mixed="true">
      <xs:restriction base="xs:anyType">
        <xs:sequence>
          <xs:any processContents="lax"
                  namespace="http://www.w3.org/1999/xhtml"
                  minOccurs="0"
                  maxOccurs="unbounded"/>
        </xs:sequence>
      </xs:restriction>
    </xs:complexContent>
  </xs:complexType>
  </xs:element>

当然,如果他只是将元素插入到您的 xml 中,您可能还想更改混合内容类型.

Of course, you might also want to change that mixed content type if he is just inserting elements into your xml.

这篇关于在 XML 文档中嵌入 XHTML 片段,并根据模式进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆