Skip to content
Advertisement

xslt node replace then ingest with javascript

I replace in memory XML node based on specific path before ingestion into NoSQL (marklogic) database.

Input: /doc1.xml

<image xmlns="http://coin/decimal">
      <DE>
         <denomination>1pf</denomination>
            <reverse>rye stalks</reverse>
            <obverse>oak sprig</obverse>
            <before>Anglo–Saxons</before>
      </DE>
      <GBP>
          <denomination>1p</denomination>
            <reverse>Arms</reverse>
            <obverse>Queen</obverse>
            <before>Anglo–Saxons</before>
      </GBP>
</image>

I replace the /before:image/before:DE/before:before value to a parameter value Xsl:

const beforeXsl =
 fn.head(xdmp.unquote(
`  <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:before="http://coin/decimal"  version="2.0">
 
    <xsl:template match="/Q{http://coin/decimal}image/Q{http://coin/decimal}DE/Q{http://coin/decimal}before">
            <xsl:element name="{local-name()}">
                <xsl:value-of select="$replace"/>
            </xsl:element>
    </xsl:template>
 
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
 
  </xsl:transform>
`));
 
xdmp.xsltEval(beforeXsl, doc, params)

Expected output:

<image xmlns="http://coin/decimal">
      <DE>
         <denomination>1pf</denomination>
            <reverse>rye stalks</reverse>
            <obverse>oak sprig</obverse>
            <before>Anglo-Dutch</before>
      </DE>
      <GBP>
          <denomination>1p</denomination>
            <reverse>Arms</reverse>
            <obverse>Queen</obverse>
            <before>Anglo–Saxons</before>
      </GBP>
</image>

I try to parameterize my xsl, but got the error:

[javascript] XSLT-BADPATTERN: MarkLogic extension syntax used, EQNames are not supported in XSLT mode

Advertisement

Answer

Why! Shouldn’t it have been

var params = {};
params.nsProduct = "http://coin/decimal"
params.qPath = "/before:image/before:DE/before:before"
params.replaceValue="Anglo-Dutch"

const implReplace = fn.head(xdmp.unquote(
`
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:before="http://coin/decimal"  
    exclude-result-prefixes="#all" version="2.0">
    
    <xsl:param name="nsProduct"/>
    <xsl:param name="qPath"/>
    <xsl:param name="replaceValue" as="xs:anyAtomicType"/>
    
    <xsl:template match="node()[(xdmp:path(.) eq $qPath)]">
        <xsl:variable name="replace">
            <xsl:element name="{local-name()}" namespace="{$nsProduct}">
                <xsl:value-of select="$replaceValue"/>
            </xsl:element>
        </xsl:variable>
        <xsl:sequence select="$replace"/>
    </xsl:template>
    
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    
</xsl:transform>

`));

xdmp.xsltEval(implReplace, doc, params)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement