Page 1 of 1
Update Values in xml files
Posted: Wed Oct 25, 2023 10:31 am
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.
Re: Update Values in xml files
Posted: Wed Oct 25, 2023 10:43 am
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>
Re: Update Values in xml files
Posted: Wed Oct 25, 2023 10:45 am
by foxpalace
Hi, whitout npm install path und fs-extra no chance
Re: Update Values in xml files
Posted: Wed Oct 25, 2023 2:21 pm
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>
Re: Update Values in xml files
Posted: Wed Oct 25, 2023 2:28 pm
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
Re: Update Values in xml files
Posted: Wed Oct 25, 2023 4:30 pm
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.