Explicit cast to date in Xpath 2.0- unknown namespace prefix? - xslt-2.0

I have created an XSLT 2.0 stylesheet and want to explicitly cast a string to a date using xs:date.
Specifically looks like this:
<xsl:value-of select="format-dateTime( xs:date(concat (EDI_DC40/CREDAT,EDI_DC40/CREDAT, 'T', EDI_DC40/CREDAT,EDI_DC40/CRETIM), '[Y0001]-[M01]-[D01]T[h1]:[m01]:[s01]')"/>
Then a validation message is thrown indicating that the namespace prefix xs: is not defined.
Any idea what needs to be done or what is wrong?

You need to declare the XML Schema namespace in your stylesheet in order to use the functions in the XML Schema namespace. It isn't declared and available by default.
xmlns:xs="http://www.w3.org/2001/XMLSchema"

Related

How do I define a XSD like "simple type" in Swagger?

I've got a type that is just a string in my yml definition:
PlayMode:
type: string
I'd like to use in my generated code, but I get a concrete class. I'm assuming I've not set-up the yml correctly.
In an XSD, this would be a "simpleType".
Just had the same issue, but looks like it has just been fixed by the swagger team: https://github.com/swagger-api/swagger-parser/issues/243

Orbeon formatting.xpl to show xmlns declarations?

Code
<xforms:output mediatype="text/html" value="xxforms:serialize(xxforms:call-xpl('oxf:/ops/utils/formatting/format.xpl', 'data', instance('message-instance'), 'data')/*, 'xml')"/>
Input
<REPC_IN000023NL xmlns="urn:hl7-org:v3">
....
<hl7:patientID xmlns:hl7="urn:hl7-org:v3">
<hl7:value root="2.16.840.1.113883.2.4.6.3" extension="999999035"/>
</hl7:patientID>
....
</REPC_IN000023NL>
Expected a rendering that includes the namespace declaration, but instead the rendering omits exactly that.
Bug or feature?
Indeed, the namespace declaration for the hl7 prefix, which isn't on the root element, is not produced:
I'd recommend you don't use that format.xpl; it isn't maintained, has performance issues with large documents, and, as you noticed, doesn't handle some corner cases well (i.e. it's buggy!).
Instead, you can use the fr:code-mirror component, which delegates formatting to CodeMirror. It won't do the indenting for you, but Saxon can do it for you. This example will give you the following output:

XSLT functions with an object as params

I've been going through some of our XSLT code and I'm starting to notice quite a bit of duplicate code. As I do like code DRY and am an OO guy, I would like to make some simple functions and call them for any of this duplicate logic. My issue is that in some cases I'd be passing in 10 parameters or some such where as another call would only be passing in 4. I know you can default parameters, but having 10 parameters to a function is just ugly. In a normal OO programming language, you'd create an object with 10 properties and pass that in which allows for the properties to grow without having to change any callers or the signature of the question in general. I know we have complex types in xsl. So should I create a separate xsd for these functions and then pass it in like:
<xsl:function name="foo:doSomething">
<xsl:param name="someComplexType"/>
...
<xsl:variable name="field1" select="someComplexType/field1" />
</xsl:function>
But then how should I build my new type with some 10 different selectors? Is this even the best way to do this? Or should I just pass in all 10 params? Conceptually the 10 different params are related to one object so it makes sense and seems cleaner to do it this way, but I'm still looking for more of a best practice for xslt going forward.
If you want to define a complex type with a schema and use that in XSLT then you need a schema-aware XSLT 2.0 or 3.0 processor like Saxon 9 Enterprise Edition.
You can however pass in an element with sub elements, without using schemas, as with XSLT 2.0 you can easily construct temporary XML nodes in your XSLT code, as in
<xsl:variable name="foo1" as="element(foo)">
<foo>
<bar><xsl:value-of select="whatever"/></bar>
<baz><xsl:value-of select="whatelse"/></baz>
...
</foo>
</xsl:variable>

MSXML - Namespaces info not persisting

I am using MSXML 6.0 to perform a transform of my own XML into another XML format. I am not sure if I maybe just don't understand exactly how MSXML works, but I believe I have noticed some strange behaviour with it....
I am adding in the namespaces to my XML doc using the setProperty method e.g.
XmlDocument.setProperty('SelectionNamespaces',
' xmlns:ms=''http://mydomain.com/2010/MySchema''');
Then I am building up the XML using my own custom serializer in memory (not saving to disk). Once serialized I then load in the XSLT file and perform the transformation using transformNodeToObject e.g.
AppXmlDoc.transformNodeToObject(XslXmlDoc, AStreamForTransformedXml);
The problem is the transform is working but none of the specific template matching XPath I have in it is. I eliminated any problems with the XSLT file itself by running it with test data through Visual Studio and it worked as expected. I then assumed it must have been an encoding issue so I made sure that all the documents involved were being read/written out as UTF-8....still no luck.
Here is an example of what the transform looks like:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="http://mydomain.com/2010/MySchema" exclude-result-prefixes="ms">
<xsl:template match="/">
<ARoot>
<head>
<xsl:apply-templates select="ms:Element/ms:SubElement" />
</head>
<body>
<xsl:apply-templates select="ms:Element/ms:DifferentSubElement" />
</body>
</ARoot>
So the result of the transform when run through MSXML brings over the basic structure but does not include any of the template data. After some testing I discovered the only way to get it to work is to do it in the following steps:
Create a new XML doc
Set the namespace info using setProperty
Serialize the XML and save to disk.
Close the document - extra step
Create a new Xml doc - extra step
Reload the document - extra step
Re-set the namespace info - extra step
Perform the transform.
So it appears that MSXML loses track of the Namespace information at somepoint. What makes it even more weird is even if you reset the namespace info (after serializing) and try the transform it still doesn't work! It will only seem to work if I save the document, close it and recreate a new XML document and load it back in (which as a consequence I need to reset the namespaces).
Anyone have any thoughts/ideas on this?
The SelectionNamespace property is used for XPath with the selectNodes and selectSingleNode methods, not for XSLT or other purposes. So I am not sure why you set it and what you expect it to help with as you do not seem to use selectNodes or selectSingleNode.
What is it that you want to achieve with MSXML 6? If you want to create some MSXML DOM documents with elements and/or attributes in namespaces then make sure you use createNode as that is the only namespace aware method in MSXML's API.

BizTalk namespace default prefix

BizTalk produced namespace default prefix as NS0. Is there a way to override it to generate something different without utilizing an XSLT (it's an overhead when the schema changes, XSLT has to be updated as well)? Something like
<pidx:Invoice>
and not
<ns0:Invoice>
This should be simple (after all it's just a namespace prefix), but clients are parsing document instead of using it as an XML with namespace, and having no expected namespace prefix causes them grief.
Thank you.
No, there's not.
A kludgey answer is to convert the XML to a string and search/replace.
You can do this. I had to do this when calling a Google API, which requires specific namespace prefixes.
The method is to use the Imports property at the <Schema> level in your XSD. Using the Imports dialog you can define specific prefixes on each namespace.
You will have to break up your schema into at least two separate XSD's so you can import one into the other and redefine the prefix at that time.

Resources