[MarkLogic Dev General] XSLT insted of XQuery in ML7

Evan Lenz evan at lenzconsulting.com
Wed Jan 27 11:14:11 PST 2016


Hi Rahul,

Your code will only create as many <Inv> elements as you have in the input
(due to your use of <xsl:for-each select="IN/Inv">, which contains just one
<Inv>). You would instead need to do an <xsl:for-each> on the tokenized
string to get the right number of <Inv> elements in the result.

Here's an alternative solution you could try. It has three rules: one rule
for <Inv> (to replace it with as many <Inv> elements as there are
comma-separated values in its first element child); one rule for <a>, <E>,
and <s> (to copy the element, extracting the value according to the
position of the current <Inv> being replicated); and one default rule (the
identity transform, for copying everything unchanged):

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs">

  <xsl:template match="Inv">
    <xsl:variable name="this-node" select="."/>
    <xsl:for-each select="tokenize(*[1],',')">
      <xsl:apply-templates mode="replicate" select="$this-node">
        <xsl:with-param name="pos" select="position()" tunnel="yes"/>
      </xsl:apply-templates>
    </xsl:for-each>
  </xsl:template>

  <xsl:template mode="replicate" match="a | E | s">
    <xsl:param name="pos" tunnel="yes"/>
    <xsl:copy>
      <xsl:value-of select="tokenize(.,',')[$pos]"/>
    </xsl:copy>
  </xsl:template>

  <!-- By default, copy everything unchanged -->
  <xsl:template mode="#default replicate" match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates mode="#current" select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>


Evan Lenz
President, Lenz Consulting Group, Inc.
http://lenzconsulting.com


On Wed, Jan 27, 2016 at 10:39 AM, Rahul Singh <rahulsinghindia15 at gmail.com>
wrote:

> Hello,
>
> I have one requirement which is need to impelment in XSLT not in XQuery in
> ML7, but I need output as per given output, but it is not coming as per
> given:
>
> Input is:
>
> <IN>
>   <Inv>
>         <a>456,3F</a>
>         <E>7a,8F</E>
>         <s>M,c</s>
>      </Inv>
> </IN>
>
> Expected Output:
>
> <IN>
>    <Inv>
>      <a>456</a>
>         <E>7a</E>
>         <s>M</s>
>    </Inv>
>    <Inv>
>     <a>3F</a>
>         <E>8F</E>
>         <s>c</s>
>    </Inv>
> </IN>
>
>
> XSLT is :
>
>     <xsl:template match="/">
>         <IN>
>             <xsl:for-each select="IN/Inv">
>                 <Inv>
>                     <xsl:for-each select="tokenize(a, ',')">
>                         <xsl:element name="a">
>                             <xsl:value-of select="."/>
>                         </xsl:element>
>                         <xsl:if test="position() != last()"> </xsl:if>
>                     </xsl:for-each>
>                      <xsl:for-each select="tokenize(s, ',')">
>                         <xsl:element name="s">
>                             <xsl:value-of select="."/>
>                         </xsl:element>
>                         <xsl:if test="position() != last()"> </xsl:if>
>                     </xsl:for-each>
>                     <xsl:for-each select="tokenize(E, ',')">
>                         <xsl:element name="E">
>                             <xsl:value-of select="."/>
>                         </xsl:element>
>                         <xsl:if test="position() != last()"> </xsl:if>
>                     </xsl:for-each>
>                 </Inv>
>             </xsl:for-each>
>         </IN>
>     </xsl:template>
>
>
> _______________________________________________
> General mailing list
> General at developer.marklogic.com
> Manage your subscription at:
> http://developer.marklogic.com/mailman/listinfo/general
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://developer.marklogic.com/pipermail/general/attachments/20160127/f9adf818/attachment-0001.html 


More information about the General mailing list