this is a german Web-Mirror of MHONARC.ORG powered by Domainunion AG

[Top] [All Lists]

Re: [xsl] How to check an element's type against an XSD simpleType and skip that element if it does not conform to the simpleType?

2013-07-16 07:25:47
Costello, Roger L. wrote:
Michael Kay wrote:

Unfortunately schema-aware processing in XQuery and XSLT
doesn't give you this capability.

Is there a workaround?

Is there is no way for an XSLT program to express: "Hey, validate xyz against the 
XML Schema and if xyz is not schema-valid then discard it." No way to express that?

I think what you can do is not to validate the input XML but to import a schema and then you can write checks using e.g.
  foo castable as someType
to filter out valid or invalid items.

So with AltovaXML, when I process the  input

<BookStore xmlns="";>
                <Title>My Life and Times</Title>
                <Author>Paul McCartney</Author>
                <Publisher>McMillan Publishing</Publisher>
          <Title>Killing Time</Title>

with the XSLT

<xsl:transform xmlns:xsl="";

    <xsl:output method="xml"/>

    <xsl:import-schema namespace="";

    <xsl:template match="/">
<xsl:for-each select="/bk:BookStore/bk:Book[bk:ISBN castable as bk:ISBN-type]">
                <ISBN><xsl:value-of select="bk:ISBN"/></ISBN>


where the schema is

<xsd:schema xmlns:xsd="";

    <xsd:element name="BookStore">
<xsd:element ref="Book" minOccurs="0" maxOccurs="unbounded"/>

    <xsd:element name="Book">
                <xsd:element ref="Title"/>
                <xsd:element ref="Author"/>
                <xsd:element ref="Date"/>
                <xsd:element ref="ISBN"/>
                <xsd:element ref="Publisher"/>

    <xsd:element name="Title" type="xsd:string"/>
    <xsd:element name="Author" type="xsd:string"/>
    <xsd:element name="Date" type="xsd:string"/>
    <xsd:element name="ISBN" type="ISBN-type"/>
    <xsd:element name="Publisher" type="xsd:string"/>

    <xsd:simpleType name="ISBN-type">
        <xsd:restriction base="xsd:string">
<xsd:pattern value="\d{1}-\d{5}-\d{3}-\d{1}|\d{1}-\d{3}-\d{5}-\d{1}|\d{1}-\d{2}-\d{6}-\d{1}"/>


then I get the result


So that way you can do some type checking against the schema type although the XML input you process is not being validated as a complete document.

XSL-List info and archive:
To unsubscribe, go to:
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>

<Prev in Thread] Current Thread [Next in Thread>