Does anyone know how to validate a DataTable against a particular DataTable XSD (Schema)?
Which would be the best performance-optimized way of doing this?
For example:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
attributeFormDefault ="qualified"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> ---Content---- </xs:schema>
Thanks
Related
I have below xml file which is coming form my vendor.
<?xml version="1.0" encoding="UTF-8"?>
<nm:MT_employee xmlns:nm="http://firstscenario.com"xmlns:tl="http://secondscenario.com">
<EMployeeDetails>
<Name>Janardhan</Name>
<id>1234</id>
<Address>India</Address>
</EMployeeDetails>
<tl:Extension>
<tl:Number>5678</tl:Number>
<tl:Salary>2345678</tl:Salary>
</tl:Extension>
</nm:MT_employee>
In the above xml I want to ignore the entire tl:Extension node. the final output should be like below
<?xml version="1.0" encoding="UTF-8"?>
<nm:MT_employee xmlns:nm="http://firstscenario.com"xmlns:tl="http://secondscenario.com">
<EMployeeDetails>
<Name>Janardhan</Name>
<id>1234</id>
<Address>India</Address>
</EMployeeDetails>
</nm:MT_employee>
I tried to with different XSLT codes but it's not working. Could you please suggest how can I achieve this?
Regards,
Janardhan
The general rule to "ignore" an element from the source XML
is to write an "empty" template for this element, in your case:
<xsl:template match="tl:Extension"/>
As this template refers to tl namespace, it must be specified
in the xsl:transform tag.
Of course, to copy the rest of the source content, your script
must include the identity template.
Below you have an example script:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tl="http://secondscenario.com" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="tl:Extension"/>
<xsl:template match="#*|node()">
<xsl:copy><xsl:apply-templates select="#*|node()"/></xsl:copy>
</xsl:template>
</xsl:transform>
Does any XSD edge case allow (unescaped) XML element content inside a text node? E.g. can you put a CDATA element inside a tag defined as xs:string and have it validate (without declaring mixed content)?
If you have an element that contains a string i.e.
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2018 (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root" type="xs:string" />
</xs:schema>
Then that can contain CDATA i.e.
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio 2018 (https://www.liquid-technologies.com) -->
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Temp\XSDFile2.xsd">
Optional Text
<![CDATA[
<someXmlData></someXmlData>
]]>
Optional Text
</Root>
As this passes though some parsers it may get escaped back to this, but both are valid and equivalent.
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio 2018 (https://www.liquid-technologies.com) -->
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="XSDFile2.xsd">
Optional Text
<someXmlData></someXmlData>
Optional Text
</Root>
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I have an XSD that I'm editing, and, using a tool such as XMLSpy or oXygen, I'd like to generate user documentation for the XSD. However, I'd like to exclude certain elements from the documentation (based on user requirements). What would be the best approach to do this?
There's no tool-independent way to exclude elements from documentation.
There's xsd:annotation/xsd:appinfo for application-oriented data.
There's xsd:annotation/xsd:documentation for human-oriented data.
XSDs are XML, so writing an XSLT transformation is certainly one way to generate documentation. Note, however, that the semantics of XSD are complex enough that the exclusion of any given element will be trivial relative to the rest of the task.
Removing elements from documentation is a three-part process:
1. Add an attribute to the the elements to indicate whether or not they should be documented.
Following is a code sample showing an XSD with three elements, and a new attribute, generateDocumentation, that indicates whether the element should be documented.
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="documentation.xslt"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mc="http://www.mycompany.com"
xsi:schemaLocation="http://www.mycompany.com ./doc.xsd"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="DocumentedElement" mc:generateDocumentation="true"/>
<xs:element name="UndocumentedElement" mc:generateDocumentation="false"/>
<xs:element name="DefaultElement"/>
</xs:schema>
Specifics on how to extend XSD with custom attributes can be found here.
Note that, in this example, elements without the generateDocumentation attribute defined will be documented by default.
2. Apply a transformation to remove elements with attribute values indicating that they should not be documented.
The following XSLT will remove elements that have mc:generateDocumentation="false" or mc:generateDocumentation="0", and will strip the resulting white space:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mc="http://www.mycompany.com">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- Identity transform -->
<xsl:template match="/ | #* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<!-- Undocumented elements -->
<xsl:template match="*[#mc:generateDocumentation='false'] | *[#mc:generateDocumentation='0']"/>
<!-- Strip white space -->
<xsl:template match="*/text()[normalize-space()]">
<xsl:value-of select="normalize-space()"/>
</xsl:template>
<xsl:template match="*/text()[not(normalize-space())]"/>
</xsl:stylesheet>
This transformation produces the XSD with the specified elements removed:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="documentation.xslt"?>
<xs:schema xmlns:mc="http://www.mycompany.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mycompany.com ./doc.xsd"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="DocumentedElement" nc:generateDocumentation="true"/>
<xs:element name="DefaultElement"/>
</xs:schema>
3. Use the tool of your choice to generate the user documentation.
I have a problem, I have an XSD file which I am trying to validate with an XML:
The XSD starts with:
<xs:schema id="Notes"
targetNamespace="http://mynotes.com/Notes"
elementFormDefault="qualified"
xmlns="http://mynotes.com/Notes"
xmlns:mstns="http://mynotes.com/Notes"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
Then I have the following XML:
<?xml version="1.0" encoding="utf-8"?>
<notes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="noNamespaceSchemaLocation" xmlns="http://mynotes.com/Notes">
In that case I can validate, but when I use JAXB to create an objects, the objects don't get populated and all their members are null.
But, If I modify the XML to the following:
<?xml version="1.0" encoding="utf-8"?>
<notes>...
In this case the JAXB objects are created successfully, but the validation fails...
I guess I am missing something with the namespace declaration, your help will be highly appreciated.
Thanks.
You can use the package level #XmlSchema annotation to specify the default namespace qualification for your JAXB model. Below is an example, you will need to adjust the package to be the same as your domain classes.
com/example/package-info.java
#XmlSchema(
namespace = "http://mynotes.com/Notes",
elementFormDefault = XmlNsForm.QUALIFIED)
package com.example;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
For More Information
http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
i have one wsdl file which have schhemas included in it, i want to generate java classes for each schema in different package,please help me.
i have used following code,its generating classes related to all schemas
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="1.0"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<jaxb:bindings schemaLocation="Event.xsd" node="/xsd:schema">
<jaxb:schemaBindings>
<jaxb:package name="com.event.dto" />
<jaxb:nameXmlTransform>
<jaxb:typeName suffix="CHASE"/>
</jaxb:nameXmlTransform>
For each shema file add xml like in your question, but with different name of
<jaxb:package name="com.event.dto" />