Update Values in xml files

Post Reply
tomgoertz
Newbie
Posts: 4
Joined: Thu Jan 31, 2019 9:18 am

Update Values in xml files

Post by tomgoertz »

How can I change a value in an xml file without using npm packages?

Example:

Code: Select all

<order>
<jobs>
<job>
	<jobID>A</jobID>
	<fileName>*/abcdef.pdf</fileName>
</job>
<job>
	<jobID>B</jobID>
	<fileName>*/abcdef.pdf</fileName>
</job>
</jobs>
</order>
Job A and B have the same file name. In my flow the files are renamed to JobID_FileName. Now I would like to change the file name in the XML accordingly.
sander
Advanced member
Posts: 276
Joined: Wed Oct 01, 2014 8:58 am
Location: The Netherlands

Re: Update Values in xml files

Post by sander »

I would go with Saxon and an xslt:

Input:

Code: Select all

<order>
<jobs>
<job>
	<jobID>A</jobID>
	<fileName>*/abcdef.pdf</fileName>
</job>
<job>
	<jobID>B</jobID>
	<fileName>*/abcdef.pdf</fileName>
</job>
</jobs>
</order>
XSLT:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- Identity template to copy everything as-is -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- Match the 'fileName' elements for 'job' elements with jobID 'B' -->
  <xsl:template match="job[jobID = 'B']/fileName">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:text>nieuw_something.pdf/</xsl:text>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
Output:

Code: Select all

<?xml version="1.0"?><order>
<jobs>
<job>
	<jobID>A</jobID>
	<fileName>*/abcdef.pdf</fileName>
</job>
<job>
	<jobID>B</jobID>
	<fileName>nieuw_something.pdf/</fileName>
</job>
</jobs>
</order>
User avatar
foxpalace
Member
Posts: 33
Joined: Fri Jan 14, 2011 12:25 pm
Location: Germany

Re: Update Values in xml files

Post by foxpalace »

Hi, whitout npm install path und fs-extra no chance
tomgoertz
Newbie
Posts: 4
Joined: Thu Jan 31, 2019 9:18 am

Re: Update Values in xml files

Post by tomgoertz »

Thanks for the quick feedback!

@foxpalace:
I actually already assumed that.

@sander:
Thanks for the idea with xslt. I just did some research and think I can solve my problem with xslt 1.0. It was a bit tricky to give the filename a prefix, since the original value also contains the complete path.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- Identity template to copy everything as-is -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- Match the 'fileName' elements -->
  <xsl:template match="fileName">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:value-of select="concat(substring-before(., concat(../jobID, '\')), ../jobID, '\', ../jobID, '_', substring-after(., concat(../jobID, '\')))"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
sander
Advanced member
Posts: 276
Joined: Wed Oct 01, 2014 8:58 am
Location: The Netherlands

Re: Update Values in xml files

Post by sander »

Great :)

Just to be sure you know this: Switch XSLT transform only supports XSLT 1.0.

If you go with the Saxonica Saxon configurator you can use 2.0 and 3.0 too:
https://www.enfocus.com/en/appstore/pro ... nica-saxon
freddyp
Advanced member
Posts: 1023
Joined: Thu Feb 09, 2012 3:53 pm

Re: Update Values in xml files

Post by freddyp »

it is a bit of a "dirty" method, but the app "String Replace" can perhaps also do the trick if you define the substitution in the right way.
Post Reply