Subject: RE: [xsl] Xpath/XSLT 2.0 & Schema Question From: "Xia Li" <xli@xxxxxxxxxxxxx> Date: Fri, 2 Apr 2010 11:45:32 -0700 |
Hi, You asked, [A quick question, when I tried to get the type-name using the fully namespaced name... <xsl:variable name="type-name" select="./@xsi:type"/> ...nothing was returned, why?] The value of the attribute @xsi:type is a reference to the type. It usually appears as a qualified name which is prefix + ":" + localname, e.g. pre:typename. In your schema, the value of the attribute @name on a type definition only contains the local name of the type, <xsd:complexType name="typename"> ... </xsd:complexType> So the path expression "$schemas//xsd:complexType[@name = $type-name]" would result in nothing as the left side of the "=" is a local name "typename" while the right side is a qualified name "pre:typename". You could construct a type QName from the qualified name provided by @xsi:type using the constructor xsd:QName(@xsi:type) and then retrieve the local name using the function local-name-from-QName(). <!-- construct a type QName from a type lexical QName --> <xsl:variable name="type-QName" select="xsd:QName(./@xsi:type)"/> <!-- retrieve the type local name from the type QName --> <xsl:variable name="type-localname" select="local-name-from-QName($type-QName)"/> <!-- use the type local name to match the complex type definition with a name attribute specifying the type local name--> <xsl:variable name="schema-obj" select="$schemas//xsd:complexType[@name = $type-localname]"/> The alternative is to directly call the function in the library xsdf:get-global-type-def($type-QName, $schemas), pass the type QName to the function as the value of the first parameter, It would return the type definition element with the specified type QName. Lisa -----Original Message----- From: Luke Stedman [mailto:luke.stedman@xxxxxxxxx] Sent: Friday, April 02, 2010 2:58 AM To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx Subject: Re: [xsl] Xpath/XSLT 2.0 & Schema Question Xia, Thanks, that seems to have done the trick. <xsl:variable name="schemas" select="xsdf:load-schemas-by-URI('schema.xsd')"/> ... <xsl:template match="*[@name]"> <xsl:variable name="type-name" select="./@*:type"/> <xsl:variable name="schema-obj" select="$schemas//xsd:complexType[@name = $type-name]"/> <xsl:variable name="class-name" select="$schema-obj//classes/base"/> ... The above works perfectly for what I want to achieve, i.e. get the base class out of the schema. A quick question, when I tried to get the type-name using the fully namespaced name... <xsl:variable name="type-name" select="./@xsi:type"/> ...nothing was returned, why? I am using Saxon 9.2 via .Net. Michael, Your comment with regards schema awareness and information being used by the XSLT processor, what information is used and where/how? Thanks for your help/pointers. Luke On 1 April 2010 17:41, Xia Li <xli@xxxxxxxxxxxxx> wrote: > > Hi > > The element test "element(*, $type)" in your code matches an *instance* element node whose type definition is of $type or derived from $type. It cannot be used to retrieve the corresponding type definition in your schema. > > If you need to make use of schema information, you might take a look at the library here http://www.ibm.com/developerworks/library/x-schemanode/. There is a function xsdf:get-global-type-def() in the library that can be used to retrieve the type definition with a given type name from your schema. > > Lisa > > -----Original Message----- > From: Luke Stedman [mailto:luke.stedman@xxxxxxxxx] > Sent: Thursday, April 01, 2010 8:59 AM > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx > Subject: [xsl] Xpath/XSLT 2.0 & Schema Question > > Apologies in advance for what is probably a very simple/basic/stupid question. > > I have just started using XSL 2.0, having had enough of the > restrictions of XSL 1.0, and am trying to write a XSL that is schema > aware. > > I would like to process an XML file and add information/process > something based on the schema, for example adding class information to > XML output or processing an element in a specific way if we want a > console script or Python script output. > > I have tried for the last couple of hours to get the code working but > seem to be unable to, a small snippet of code is below... > > <xsl:template match="*[@name and @xsi:type]"> > <xsl:copy> > <xsl:copy-of select="@*"/> > <xsl:attribute name="action" select="'new'"/> > <xsl:variable name="name" select="name(.)"/> > <xsl:variable name="type" select="current()/@xsi:type"/> > <xsl:variable name="object" select="element(*, $type)"/> <!-- I > want to get the relevant schema entry here --> > <xsl:message> > ++++ > <xsl:value-of select="current()/@name"/> > <xsl:variable name="type" select="current()/@xsi:type"/> > <xsl:value-of select="$object//base"/> > ++++ > </xsl:message> > <xsl:apply-templates/> > </xsl:copy> > </xsl:template> > > Am I misunderstanding the functionality or doing something wrong? > > If I have missed a relevant post on the list or obvious link then > please point me in its direction, I was unable to find one using a > search. > > Apologies again for any stupid comments/expectations. > Luke
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Xpath/XSLT 2.0 & Schema Q, Luke Stedman | Thread | RE: [xsl] Xpath/XSLT 2.0 & Schema Q, Michael Kay |
RE: [xsl] conditional based html fo, Michael Kay | Date | [xsl] Replace nodes in xml file, Suresh |
Month |