سؤال

In my XSLT script I'm calling a recursive template and I want to pass a xPath as a parameter to each iteration. In each iteration I want to concatenate child's xPath(*/) to the current path (please refer the code). So I'm using concat() function, since it returns a string, I'm unable to use that path to print the content of that path.

<xsl:copy-of select="$path" /> <!--This requests a xPath, not a string-->

So can any one tell me how to concatenate two xpaths or how to convert a string to a xpath.

Thank you.

    <xsl:template match="/">
        <xsl:call-template name="repeatable" >
            <xsl:with-param name="limit" select="10" />
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="repeatable">
        <xsl:param name="index" select="1" />
        <xsl:param name="limit" select="1" />
        <xsl:param name="path" select="@*" />

        <xsl:copy-of select="$path" />

        <xsl:if test="($limit >= $index)">
            <xsl:call-template name="repeatable">
                <xsl:with-param name="index" select="$index + 1" />
                <xsl:with-param name="path" select="concat('*/', $path)" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
هل كانت مفيدة؟

المحلول

While I'm waiting for you to respond to my question above, here's an XSLT that does what you seem to be trying to do:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="@*">
    <xsl:value-of select="concat(name(), ' = ', ., '&#xA;')"/>
  </xsl:template>

  <xsl:template match="/">
    <xsl:call-template name="repeatable" >
      <xsl:with-param name="limit" select="10" />
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="repeatable">
    <xsl:param name="index" select="1" />
    <xsl:param name="limit" select="1" />
    <xsl:param name="current" select="." />

    <xsl:apply-templates select="$current/@*" />

    <xsl:if test="($limit >= $index)">
      <xsl:call-template name="repeatable">
        <xsl:with-param name="index" select="$index + 1" />
        <xsl:with-param name="limit" select="$limit" />
        <xsl:with-param name="current" select="$current/*" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

When this is run on the following input:

<root a1="a" a2="b">
  <cont a3="c" a4="d">
    <child a5="e" a6="f" />
  </cont>
  <cont a7="g" a8="h">
    <child a9="i" a10="j">
      <subchild a11="k" a12="l" />
    </child>
  </cont>
</root>

The result is:

a1 = a
a2 = b
a3 = c
a4 = d
a7 = g
a8 = h
a5 = e
a6 = f
a9 = i
a10 = j
a11 = k
a12 = l

Is that close to what you are trying to do? If not, please clarify.

نصائح أخرى

What you want to do is to dynamically create xpath and use it. This is possible in XSLT3.0 and the function to be used is evaluate().

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top