Home > Enterprise >  Duplicate line and replace string
Duplicate line and replace string

Time:10-19

I have an XML file that contains more than 10,000 items. Each item contains a line like this.

<g:id><![CDATA[FBM00101816_BLACK-L]]></g:id>

For each item I need to add another line below like this:

<sku><![CDATA[FBM00101816]]></sku>

So I need to duplicate each g:id line, replace the g:id with sku and trim the value to delete all characters after the underscore (including it). The final result would be like this:

<g:id><![CDATA[FBM00101816_BLACK-L]]></g:id>
<sku><![CDATA[FBM00101816]]></sku>

Any ideas how to accomplish this?

Thanks in advance.

CodePudding user response:

In XSLT, it's

<xsl:template match="g:id">
  <xsl:copy-of select="."/>
  <sku><xsl:value-of select="substring-before(., '_')"/></sku>
</xsl:template>

Or using Saxon's Gizmo (enter image description here

  • Related