[MarkLogic Dev General] XSLT insted of XQuery in ML7

Geert Josten Geert.Josten at marklogic.com
Wed Jan 27 11:18:49 PST 2016


Nice work Evan..

@Rahul, less elegant, but this might look more familiar for you, or easier to understand:

xquery version "1.0-ml";

let $in :=
<IN>
  <Inv>
        <a>456,3F</a>
        <E>7a,8F</E>
        <s>M,c</s>
     </Inv>
</IN>

let $expected :=
<IN>
   <Inv>
     <a>456</a>
        <E>7a</E>
        <s>M</s>
   </Inv>
   <Inv>
    <a>3F</a>
        <E>8F</E>
        <s>c</s>
   </Inv>
</IN>

let $xsl :=
<xsl:stylesheet version="2" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <IN>
            <xsl:for-each select="IN/Inv">
                <xsl:variable name="a" select="tokenize(a, ',')"/>
                <xsl:variable name="E" select="tokenize(E, ',')"/>
                <xsl:variable name="s" select="tokenize(s, ',')"/>

                <xsl:for-each select="1 to count($a)">
                  <xsl:variable name="i" select="."/>
                    <Inv>
                        <xsl:element name="a">
                            <xsl:value-of select="$a[position() = $i]"/>
                        </xsl:element>
                        <xsl:element name="E">
                            <xsl:value-of select="$E[position() = $i]"/>
                        </xsl:element>
                        <xsl:element name="s">
                            <xsl:value-of select="$s[position() = $i]"/>
                        </xsl:element>
                    </Inv>
                </xsl:for-each>
            </xsl:for-each>
        </IN>
    </xsl:template>
</xsl:stylesheet>
 return xdmp:xslt-eval($xsl, document{$in})

From: <general-bounces at developer.marklogic.com<mailto:general-bounces at developer.marklogic.com>> on behalf of Evan Lenz <evan at lenzconsulting.com<mailto:evan at lenzconsulting.com>>
Reply-To: MarkLogic Developer Discussion <general at developer.marklogic.com<mailto:general at developer.marklogic.com>>
Date: Wednesday, January 27, 2016 at 8:14 PM
To: MarkLogic Developer Discussion <general at developer.marklogic.com<mailto:general at developer.marklogic.com>>
Cc: "general-owner at developer.marklogic.com<mailto:general-owner at developer.marklogic.com>" <general-owner at developer.marklogic.com<mailto:general-owner at developer.marklogic.com>>
Subject: Re: [MarkLogic Dev General] XSLT insted of XQuery in ML7

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<mailto: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<mailto: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/0eedc043/attachment.html 


More information about the General mailing list