Solved

How to insert XML fragments stored in an attribute into XML document using XMLUpdater

  • 14 February 2022
  • 3 replies
  • 22 views

Badge +10

I have this xml fragment in an attribute called distr_formats and need to insert it in a specified location of an xml document (stored in another attribute):

<gmd:distributionFormat>
    <gmd:MD_Format>
        <gmd:name>
            <gco:CharacterString>DWG</gco:CharacterString>
        </gmd:name>
        <gmd:version>
            <gco:CharacterString>r2013</gco:CharacterString>
        </gmd:version>
        <gmd:fileDecompressionTechnique>
            <gco:CharacterString>ZIP</gco:CharacterString>
        </gmd:fileDecompressionTechnique>
    </gmd:MD_Format>
</gmd:distributionFormat>
<gmd:distributionFormat>
    <gmd:MD_Format>
        <gmd:name>
            <gco:CharacterString>SHP</gco:CharacterString>
        </gmd:name>
        <gmd:version>
            <gco:CharacterString>-</gco:CharacterString>
        </gmd:version>
        <gmd:fileDecompressionTechnique>
            <gco:CharacterString>ZIP</gco:CharacterString>
        </gmd:fileDecompressionTechnique>
    </gmd:MD_Format>
</gmd:distributionFormat>

These are the parameters in my XMLUpdater transformer:

XMLUpdaterI'm getting this error and I'm not sure what I'm doing wrong:

 invalid expression: syntax error, unexpected ">"

Any idea?

Thanks!

icon

Best answer by takashi 14 February 2022, 12:36

View original

3 replies

Userlevel 2
Badge +17

Hi @dms2​ , the reason for the error is that the value of distr_formats is NOT a single XML fragment, it's just a concatenated string of two <gmd:distributionFormat> fragments.

A possible workaround is to transform the value to an XQuery sequence - comma-separated elements quoted by round brackets - before entering it to the XMLUpdater.

(
<gmd:distributionFormat><gmd:MD_Format><gmd:name><gco:CharacterString>DWG</gco:CharacterString></gmd:name><gmd:version><gco:CharacterString>r2013</gco:CharacterString></gmd:version><gmd:fileDecompressionTechnique><gco:CharacterString>ZIP</gco:CharacterString></gmd:fileDecompressionTechnique></gmd:MD_Format>
</gmd:distributionFormat>
,
<gmd:distributionFormat><gmd:MD_Format><gmd:name><gco:CharacterString>SHP</gco:CharacterString></gmd:name><gmd:version><gco:CharacterString>-</gco:CharacterString></gmd:version><gmd:fileDecompressionTechnique><gco:CharacterString>ZIP</gco:CharacterString></gmd:fileDecompressionTechnique></gmd:MD_Format>
</gmd:distributionFormat>
)

How to? For example,

StringReplacer

  • Attributes: distr_formats
  • Mode: Replace Regular Expression
  • Case Sensitive: No
  • Text To Replace: (</gmd:distributionFormat>)\s*(<gmd:distributionFormat>)
  • Replacement Text: \1,\2

StringConcatenator

  •  Expression Results: Overwrite Existing Attributes
  • Attributes To Overwrite: distr_formats
  • Concatenation: (@CurrentAttribute())

 

Userlevel 2
Badge +17

Another thought. Just setting this XQuery expression in the Value column could also be possible, instead of transforming the input "distr_formats" string with the method I mentioned above.

This expression returns a sequence consisting of the two <gmd:distributionFormat> elements.

for $x in <temp>@Value(distr_formats)</temp>/*
return $x

 

Badge +10

Thank you so much @Takashi Iijima​ .

Both options are good for me but the Xquery expression saves me from adding extra transformers to the flow.

Learning XQuery still is a pending topic I've had for years. I'd like to dig into it some day. I must say though that it's you who I must thank for the little I know of it.

Reply