I replace in memory XML node based on specific path before ingestion into NoSQL (marklogic) database.
Input: /doc1.xml
JavaScript
x
15
15
1
<image xmlns="http://coin/decimal">
2
<DE>
3
<denomination>1pf</denomination>
4
<reverse>rye stalks</reverse>
5
<obverse>oak sprig</obverse>
6
<before>Anglo–Saxons</before>
7
</DE>
8
<GBP>
9
<denomination>1p</denomination>
10
<reverse>Arms</reverse>
11
<obverse>Queen</obverse>
12
<before>Anglo–Saxons</before>
13
</GBP>
14
</image>
15
I replace the /before:image/before:DE/before:before
value to a parameter value
Xsl:
JavaScript
1
22
22
1
const beforeXsl =
2
fn.head(xdmp.unquote(
3
` <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
4
xmlns:before="http://coin/decimal" version="2.0">
5
6
<xsl:template match="/Q{http://coin/decimal}image/Q{http://coin/decimal}DE/Q{http://coin/decimal}before">
7
<xsl:element name="{local-name()}">
8
<xsl:value-of select="$replace"/>
9
</xsl:element>
10
</xsl:template>
11
12
<xsl:template match="@* | node()">
13
<xsl:copy>
14
<xsl:apply-templates select="@* | node()"/>
15
</xsl:copy>
16
</xsl:template>
17
18
</xsl:transform>
19
`));
20
21
xdmp.xsltEval(beforeXsl, doc, params)
22
Expected output:
JavaScript
1
15
15
1
<image xmlns="http://coin/decimal">
2
<DE>
3
<denomination>1pf</denomination>
4
<reverse>rye stalks</reverse>
5
<obverse>oak sprig</obverse>
6
<before>Anglo-Dutch</before>
7
</DE>
8
<GBP>
9
<denomination>1p</denomination>
10
<reverse>Arms</reverse>
11
<obverse>Queen</obverse>
12
<before>Anglo–Saxons</before>
13
</GBP>
14
</image>
15
I try to parameterize my xsl, but got the error:
JavaScript
1
2
1
[javascript] XSLT-BADPATTERN: MarkLogic extension syntax used, EQNames are not supported in XSLT mode
2
Advertisement
Answer
Why! Shouldn’t it have been
JavaScript
1
37
37
1
var params = {};
2
params.nsProduct = "http://coin/decimal"
3
params.qPath = "/before:image/before:DE/before:before"
4
params.replaceValue="Anglo-Dutch"
5
6
const implReplace = fn.head(xdmp.unquote(
7
`
8
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
9
xmlns:xs="http://www.w3.org/2001/XMLSchema"
10
xmlns:before="http://coin/decimal"
11
exclude-result-prefixes="#all" version="2.0">
12
13
<xsl:param name="nsProduct"/>
14
<xsl:param name="qPath"/>
15
<xsl:param name="replaceValue" as="xs:anyAtomicType"/>
16
17
<xsl:template match="node()[(xdmp:path(.) eq $qPath)]">
18
<xsl:variable name="replace">
19
<xsl:element name="{local-name()}" namespace="{$nsProduct}">
20
<xsl:value-of select="$replaceValue"/>
21
</xsl:element>
22
</xsl:variable>
23
<xsl:sequence select="$replace"/>
24
</xsl:template>
25
26
<xsl:template match="@* | node()">
27
<xsl:copy>
28
<xsl:apply-templates select="@* | node()"/>
29
</xsl:copy>
30
</xsl:template>
31
32
</xsl:transform>
33
34
`));
35
36
xdmp.xsltEval(implReplace, doc, params)
37