1

Im using this merge.xslt script to merge several different XML files into one. This tool is very useful and includes a dontmerge command-line parameter so that certain XML elements wont be merged. The problem is that only one XML element can be specified, and I need to be able to specify multiple XML elements that shouldnt be merged. I could just add more command-line parameters to the script, like dontmerge2, dontmerge3, etc but this isnt an elegant solution.

While studying the particular XML elements that should not be merged, I realized that they are all of the same XML schema type, as follows:

<xs:element name="doCliCommand" type="cliParametersT">
</xs:element>
<xs:element name="undoCliCommand" type="cliParametersT">
</xs:element>

So, instead of using dontmerge for both doCliCommand and undoCliCommand, I would like to be able to do something like dontmergetype=cliParametersT

The relevant part of the merge.xslt is the following:

  <xslt:param name="dontmerge" />
  ...
  <xslt:when test="$type1='element' and $type2='element' and
                   local-name($node1)=local-name($node2) and
                   namespace-uri($node1)=namespace-uri($node2) and
                   name($node1)!=$dontmerge and name($node2)!=$dontmerge">

So here is the question: How do I modify this particular xslt test condition to be able to test for the XML schema type? I havent found any appropriate XPath, XQuery, or XSLT functions to get the schema type.

There is a somewhat related question where they mention that this cant be done unless using Saxon extensions. Im using SaxonHE9-5-1-3J.

Community
  • 1
  • 1
Brady
  • 10,207
  • 2
  • 20
  • 59
  • possible duplicate of [How to check in XPATH if a type is equal to a type or derived from the type](http://stackoverflow.com/questions/8323452/how-to-check-in-xpath-if-a-type-is-equal-to-a-type-or-derived-from-the-type) – ceving Dec 19 '13 at 10:20
  • @ceving good point. I see that in the answer provided they say its not possibleto do what Im asking unless using Saxon or related extensions. I am using Saxon, so perhaps I should modify the question accordingly, because I dont know how to use these Saxon extensions. – Brady Dec 19 '13 at 10:29
  • Which XSLT processor do you use or can you use? There are schema-aware XSLT 2.0 processors that might help. MSXML 6 is an XSLT 1.0 processor but also has its own proprietary schema support, see http://msdn.microsoft.com/en-us/library/ms256453%28v=vs.110%29.aspx. – Martin Honnen Dec 19 '13 at 10:31
  • @MartinHonnen I updated the question to mention that Im using Saxon HE 9-5-1-3J. – Brady Dec 19 '13 at 10:33
  • @Brady, the Home Edition of Saxon is not schema-aware I think. – Martin Honnen Dec 19 '13 at 10:34

1 Answers1

1

With a schema-aware XSLT 2.0 processor such as Saxon-EE, you can write

<xsl:when test=". instance of element(*, cliParametersT)">

You need to make sure that the schema is imported into the stylesheet using xsl:import-schema, and you need to make sure that the source document is actually validated against the schema (with Saxon, use -val:strict on the command line).

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Sounds like it needs to be hard-coded into the xslt, considering I also need to import the schema? I was hoping to make it configurable on the command line. – Brady Dec 19 '13 at 10:50
  • @Brady: You are using Saxon-HE which is not schema-aware. In other words, with your edition of the Saxon processor, XML cannot be validated against a schema. – Mathias Müller Dec 19 '13 at 13:11
  • Right, but what if I switch to the EE version? Would the schema still need to be imported? – Brady Dec 19 '13 at 13:23
  • Yes, adding `` as a top-level declaration to the stylesheet ("merge.xslt", I suppose) is most straightforward. Also have a look at: http://saxonica.com/documentation9.4-demo/html/schema-processing/satransformcmd.html – Mathias Müller Dec 19 '13 at 14:53
  • 1
    Testing membership of a type whose name is not known statically could be done using dynamic XPath evaluation (xsl:evaluate in 3.0, or saxon:evaluate), or using the extension function saxon:type-annotation. – Michael Kay Dec 19 '13 at 15:37