Jump to content
elzahr

Different Element Names

Recommended Posts

I think the solution to my problem is very easy, but i couldn't fint it :)So, here is:I have an XML which have a list of elements with different names, but in sequence. An example:

<DOC> <DOC_OBL_1>  <TIP_DOC_OBL>1</TIP_DOC_OBL>  </DOC_OBL_1> <DOC_OBL_2>  <TIP_DOC_OBL>2</TIP_DOC_OBL>   </DOC_OBL_2> <DOC_OBL_3>  <TIP_DOC_OBL>3</TIP_DOC_OBL>   </DOC_OBL_3></DOC>

So, i have 3 elements: DOC_OBL_1, DOC_OBL_2 and DOC_OBL_3. And yes, there could be number 4, 5, 6, etc.As you can se, all 3 have the same elements inside(actually, they have a lot of them, but arent important righ now), and I thinked i could declare a general type which could validate this kind of documents.How can i validate this with an Schema???I know its a very ugly XML (maybe it isnt standard, please tell me, i dont know), but It's not my concern to build this document. I just have to parse it, validate it and transform it.You can just link to the solution, any help will be wellcome.PD: im an english (language) student >.<

Edited by eLZahR

Share this post


Link to post
Share on other sites
I think the solution to my problem is very easy, but i couldn't fint it :)So, here is:I have an XML which have a list of elements with different names, but in sequence. An example:
<DOC> <DOC_OBL_1>  <TIP_DOC_OBL>1</TIP_DOC_OBL>  </DOC_OBL_1> <DOC_OBL_2>  <TIP_DOC_OBL>2</TIP_DOC_OBL>   </DOC_OBL_2> <DOC_OBL_3>  <TIP_DOC_OBL>3</TIP_DOC_OBL>   </DOC_OBL_3></DOC>

So, i have 3 elements: DOC_OBL_1, DOC_OBL_2 and DOC_OBL_3. And yes, there could be number 4, 5, 6, etc.As you can se, all 3 have the same elements inside(actually, they have a lot of them, but arent important righ now), and I thinked i could declare a general type which could validate this kind of documents.How can i validate this with an Schema???I know its a very ugly XML (maybe it isnt standard, please tell me, i dont know), but It's not my concern to build this document. I just have to parse it, validate it and transform it.You can just link to the solution, any help will be wellcome.PD: im an english (language) student >.<

I don't know Schema very well, but I don't think you're able to match patterns in element names.I find it more logical to have a <DOC_OBL> element and then give it an ID attribute.
<DOC_OBL ID="1">  <TIP_DOC_OBL>1</TIP_DOC_OBL> </DOC_OBL>

This way you can just check if there's a <DOC_OBL> element and that it has the ID attribute.

Share this post


Link to post
Share on other sites

While I don't consider myself an expert in XML Schema either, I can tell you for sure that it doesn't allow matching patterns in element names. I second Ingolme's alternative.

Edited by boen_robot

Share this post


Link to post
Share on other sites

Ok, thank you all.I've done this, finally:

  <xsd:element name="DOC_OBL">	<xsd:complexType>	  <xsd:sequence>		<xsd:element name="DOC_OBL_1" type="DocOBLType" minOccurs="1" />		<xsd:element name="DOC_OBL_2" type="DocOBLType" minOccurs="0" />		<xsd:element name="DOC_OBL_3" type="DocOBLType" minOccurs="0" />		<xsd:element name="DOC_OBL_4" type="DocOBLType" minOccurs="0" />		<xsd:element name="DOC_OBL_5" type="DocOBLType" minOccurs="0" />		<xsd:element name="DOC_OBL_6" type="DocOBLType" minOccurs="0" />		<xsd:element name="DOC_OBL_7" type="DocOBLType" minOccurs="0" />		<xsd:element name="DOC_OBL_8" type="DocOBLType" minOccurs="0" />		<xsd:element name="DOC_OBL_9" type="DocOBLType" minOccurs="0" />	  </xsd:sequence>	</xsd:complexType>  </xsd:element>

with this:

  <xsd:complexType name="DocOBLType">	<xsd:sequence>	  //elements..	</xsd:sequence>  </xsd:complexType>

It works, for now. ^^(No, no, no.. i cant change the document format, its not my concern!!)

Edited by eLZahR

Share this post


Link to post
Share on other sites

If the input XML generation is out of your control, you could use XSLT to convert the XML into one that would potentially be validatable with XML Schema. If the convertion produced an invalid XML, you can be sure that the XSLT input is also invalid.The XSLT template for converting the input XML could look something like:

<xsl:template match="*[starts-with(local-name(), 'DOC_OBL_')]"><DOC_OBL id="{substring-after(local-name(), 'DOC_OBL_')}"><xsl:apply-templates/></DOC_OBL></xsl:template>

Of course, you'd also need a copying template:

<xsl:template match="*|@*"><xsl:copy><xsl:apply-templates/></xsl:copy></xsl:template>

Edited by boen_robot

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...