topherg
topherg

Reputation: 4293

Converting XML into String (CDATA) in XSLT

I have been using Altova Map Force to build XSL transformations recently, and I have come across an issue. In order to create a valid XML output file (from an input text file, which was simple enough to build), it needs to contain some xmlns attribute tags.

Unfortunately, I cannot find a way to do this validly (modified the output xsd schema def and attribute forcing) within Map Force or my Data Transformation Tool (built by the client I am building this for, based on Saxon). So, my solution is to parse the generated XML into another (final) transformer where the XML Elements of the File are transformed into a single text block that contains a string representation of the XML (i.e. the file string that is returned), then trim off the parent element declaration, and modify it with the amended element (with the xmlns attribute), then output that to save.

I have done some tests, and I can do this validly, but in that test, I was parsing in a pre-converted xml string that had the function applied to it. So, can anyone advise me as to how, within xslt, I can convert a collection of elements into a string that I can modify and return as CDATA (which the transformer program will convert treat as a string and dump it straight into the output file)?

Upvotes: 0

Views: 1890

Answers (2)

topherg
topherg

Reputation: 4293

Success, I had to modify the XSD schema and set the targetNamespace to the same as xmlns in the schema, along with elementFormDefault="qualified", and that has modified the output xml to the appropriate response

Upvotes: 0

Martin Honnen
Martin Honnen

Reputation: 167471

The latest version of the Altova product line has support for XPath 3.0 functions like http://www.w3.org/TR/xpath-functions-30/#func-serialize so assuming that all you need is e.g.

<xsl:output cdata-section-elements="foo"/>

<xsl:template match="bar">
  <foo><xsl:value-of select="serialize(.)"/></foo>
</xsl:template>

Saxon's commercial versions also support that function I think (if you use version="3.0" for your stylesheet).

However you might want to post the real problem description (input you have, output you want) in a separate question so that we can see whether you really need to use the approach you have outlined so far to ensure you have certain namespace declarations.

Upvotes: 1

Related Questions