Jump to content

Different Element Names


elzahr

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 >.<

Link to comment
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.

Link to comment
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.

Link to comment
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!!)

Link to comment
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>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...