How to specify namespace prefix for only some fields

Hi,
I’m trying to get XMLWriter to produce an ATOM feed. I need something like:


<?xml version="1.0" encoding="ISO-8859-1"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:h="http://xml.company.com/data/Locale">
  <entry>
    <title>Locale 1</title>
    <h:city>Fresno</h:city>
    <h:state>California</h:state>
    <content /> 
  <entry>
</feed>

I have an input edge sending a title, city, state, and content field. I can use fieldsNamespacePrefix=“h” however this prefixes all fields including title and content which are defined in the ATOM namespace. Mapping file below:


<Mapping 
      inPort="0" 
      element="entry"
      fieldsNamespacePrefix="h">
</Mapping>

Any suggestions? Thanks.

Hello,

You can create the following graph:


<?xml version="1.0" encoding="UTF-8"?>
<Graph author="cloveruser" created="Thu Dec 10 10:11:37 CET 2009" guiVersion="0.0.0.devel" id="1260436942367" licenseType="Evaluation Devel" modified="Thu Dec 10 17:32:43 CET 2009" modifiedBy="cloveruser" name="XMLWriter" revision="1.46">
<Global>
<Metadata id="Metadata0" previewAttachmentCharset="ISO-8859-1">
<Record fieldDelimiter=";" name="recordName1" previewAttachmentCharset="ISO-8859-1" recordDelimiter="\r\n" type="delimited">
<Field name="title" type="string"/>
<Field name="city" type="string"/>
<Field name="state" type="string"/>
<Field name="content" type="string"/>
<Field auto_filling="global_row_count" name="autofilling" type="integer"/>
</Record>
</Metadata>
<Metadata id="Metadata1" previewAttachmentCharset="ISO-8859-1">
<Record fieldDelimiter="|" name="recordName2" previewAttachmentCharset="ISO-8859-1" recordDelimiter="\r\n" type="delimited">
<Field name="title" type="string"/>
<Field name="content" type="string"/>
<Field name="autofilling" type="string"/>
</Record>
</Metadata>
<Metadata id="Metadata2" previewAttachmentCharset="ISO-8859-1">
<Record fieldDelimiter="|" name="recordName3" previewAttachmentCharset="ISO-8859-1" recordDelimiter="\r\n" type="delimited">
<Field name="city" type="string"/>
<Field name="state" type="string"/>
<Field name="autofilling" type="string"/>
</Record>
</Metadata>
<Metadata id="Metadata5">
<Record fieldDelimiter="|" name="recordName6" recordDelimiter="\r\n" type="delimited">
<Field name="autofilling" type="string"/>
</Record>
</Metadata>
<Property fileURL="workspace.prm" id="GraphParameter0"/>
</Global>
<Phase number="0">
<Node enabled="enabled" fileURL="${DATAIN_DIR}/XMLinput.txt" guiHeight="0" guiName="UniversalDataReader" guiWidth="0" guiX="33" guiY="34" id="DATA_READER0" type="DATA_READER"/>
<Node enabled="enabled" guiHeight="0" guiName="Reformat" guiWidth="0" guiX="187" guiY="30" id="REFORMAT0" type="REFORMAT">
<attr name="transform"><![CDATA[//#TL

// Transforms input record into output record.
function transform() {
	$1.title := $0.title;
	$0.city := $0.city;
	$0.state := $0.state;
	$1.content := $0.content;
	$0.autofilling := $0.autofilling;
	$1.autofilling := $0.autofilling;
	$2.autofilling := $0.autofilling;
}

// Called to return a user-defined error message when an error occurs.
// function getMessage() {}

// Called during component initialization.
// function init() {}

// Called after the component finishes.
// function finished() {}
]]></attr>
</Node>
<Node enabled="enabled" fileUrl="${DATAOUT_DIR}/output.xml" guiHeight="0" guiName="XMLWriter" guiWidth="0" guiX="388" guiY="29" id="XML_WRITER0" rootDefaultNamespace="http://www.w3.org/2005/Atom" rootElement="feed" rootInfoAttributes="false" rootNamespaces="h=&quot;http://xml.company.com/data/Locale&quot;" type="XML_WRITER">
<attr name="mapping"><![CDATA[<Mapping inPort="2" 
         element="entry" 
         fieldsIgnore="autofilling">
  <Mapping
      inPort="1"
      element="non-prefixed"
      key="autofilling" parentKey="autofilling"
      fieldsIgnore="autofilling">
  </Mapping>
  <Mapping
      inPort="0"
      element="prefixed"
      fieldsNamespacePrefix="h"
      key="autofilling" parentKey="autofilling"
      fieldsIgnore="autofilling">
  </Mapping>
</Mapping>]]></attr>
</Node>
<Edge debugMode="true" fromNode="DATA_READER0:0" guiBendpoints="" id="Edge0" inPort="Port 0 (in)" metadata="Metadata0" outPort="Port 0 (output)" toNode="REFORMAT0:0"/>
<Edge debugMode="true" fromNode="REFORMAT0:0" guiBendpoints="" id="Edge2" inPort="Port 0 (in)" metadata="Metadata2" outPort="Port 0 (out)" toNode="XML_WRITER0:0"/>
<Edge debugMode="true" fromNode="REFORMAT0:1" guiBendpoints="" id="Edge1" inPort="Port 1 (in)" metadata="Metadata1" outPort="Port 1 (out)" toNode="XML_WRITER0:1"/>
<Edge debugMode="true" fromNode="REFORMAT0:2" guiBendpoints="" id="Edge3" inPort="Port 2 (in)" metadata="Metadata5" outPort="Port 2 (out)" toNode="XML_WRITER0:2"/>
</Phase>
</Graph>

This way, you cannot create exactly what you wanted, however, you can distinguish the prefixed and the non-prefixed elements by sending them into different ports.

The resulting file looks like this:


<?xml version="1.0" encoding="ISO-8859-1"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:h="http://xml.company.com/data/Locale">
<entry>
<non-prefixed>
<title>mytitle</title>
<content>mycontent</content>
</non-prefixed>
<prefixed>
<h:city>mycity</h:city>
<h:state>mystate</h:state>
</prefixed>
</entry>
</feed>

You can get all the information you want, some elements are prefixed, the others are not.

Should you wanted to remove the and elements, you should maybe process the file with the XSLTransform component.

Best regards,

Tomas Waller

Hmm, that’s what I was afraid of. If I have to go through a transform phase anyway to remove the prefix and non-prefix elements, I could add the prefix to all elements and then use the transform to rip it off the ATOM specific elements (ala s/<h:title/<title/). Then at least I don’t have to split my data into two ports and re-join it.

Thanks.