В XSLT, как я увеличиваю глобальную переменную от другого объема?

изменить:

изменить это в JavaScript:

var $j = jQuery.noConflict();

$j(function () {

    $j(".wpb_wrapper").click(function () {

        var fgfdgds = $j(this).attr('value');

        var formData = new FormData();
        formData.append("post_id", 'fgfdgds');
        formData.append("action", 'my_action');


        $j('.modal-body').attr('value', fgfdgds);

        $j.ajax({
            url: custom_script_object.ajax_url,
            type: 'post',
            data: formData,
            processData: false,
            contentType: false,
            success: function (response) {
                $j('.rml_contents').html(response);
                console.log("it worked");
            }
        });



    });
});
27
задан Marcel 7 May 2009 в 06:15
поделиться

7 ответов

Others have already explained how variables are immutable--that there are no assignment statements in XSLT (as with purely functional programming languages in general).

I have an alternative to the solutions that have been proposed so far. It avoids parameter passing (which is verbose and ugly in XSLT--even I'll admit that).

In XPath, you can simply count the number of

elements that precede the current one:

<xsl:template name="section">
  <span class="title" id="title-{1 + count(preceding-sibling::section)}">
    <xsl:value-of select="title"/>
  </span>
</xsl:template>

(Note: the whitespace code formatting won't appear in your result, as whitespace-only text nodes get stripped from the stylesheet automatically. So don't feel compelled to put instructions on the same line.)

One big advantage of this approach (as opposed to using position()) is that it's only dependent on the current node, not on the current node list. If you changed your processing somehow (e.g., so processed not only sections but some other element too), then the value of position() would no longer necessarily correspond to the position of

elements in your document. On the other hand, if you use count() as above, then it will always correspond to the position of each
element. This approach reduces coupling with other parts of your code, which is generally a very good thing.

An alternative to count() would be to use the instruction. It's default behavior will number all like-named elements at the same level, which happens to be what you want:

<xsl:template name="section">
  <xsl:variable name="count">
    <xsl:number/>
  </xsl:variable>
  <span class="title" id="title-{$count}">
    <xsl:value-of select="title"/>
  </span>
</xsl:template>

It's a trade-off in verbosity (requiring an additional variable declaration if you still want to use the attribute value template curly braces), but only slightly so, as it also drastically simplifies your XPath expression.

There's yet more room for improvement. While we've removed dependency on the current node list, we still are dependent on the current node. That, in and of itself, is not a bad thing, but it's not immediately clear from looking at the template what the current node is. All we know is that the template is named "section"; to know for sure what's being processed, we have to look elsewhere in our code. But even that doesn't have to be the case.

If you ever feel led to use and together (as in your example), step back and figure out how to use instead.

<xsl:template match="/doc">
  <xsl:apply-templates select="section"/>
</xsl:template>

<xsl:template match="section">
  <xsl:variable name="count">
    <xsl:number/>
  </xsl:variable>
  <span class="title" id="title-{$count}">
    <xsl:value-of select="title"/>
  </span>
</xsl:template>

Not only is this approach less verbose ( replaces both and ), but it also becomes immediately clear what the current node is. All you have to do is look at the match attribute, and you instantly know that you're processing a

element and that
elements are what you're counting.

For a succinct explanation of how template rules (i.e. elements that have a match attribute) work, see "How XSLT Works".

44
ответ дан 28 November 2019 в 04:42
поделиться

XSLT variables cannot be changed. You'll have pass the value along from template to template.

If you are using XSLT 2.0, you can have parameters and use tunneling to propagate the variable to the right templates.

Your template will look something like this:

<xsl:template match="a">
<xsl:param name="count" select="0">
  <xsl:apply-templates>
     <xsl:with-param select="$count+1"/>
  </xsl:apply-templates>
</xsl:template>

Also look at using generate-id() if you want to create ids.

9
ответ дан 28 November 2019 в 04:42
поделиться

variables are locally scoped and read only in xslt.

2
ответ дан 28 November 2019 в 04:42
поделиться

В зависимости от вашего процессора XSLT, вы можете использовать скриптовые функции в своем XLST. Например, библиотека Microsoft XML поддерживает включение javascript. См. http://msdn.microsoft.com/en-us/library/aa970889 (VS.85) .aspx для примера. Эта тактика, очевидно, не сработает, если вы планируете развернуть / выполнить XSLT в общедоступных клиентских браузерах; это должно быть сделано определенным процессором XSLT.

2
ответ дан 28 November 2019 в 04:42
поделиться

You can use the position() function to do what you want. It would look something like this.

<xsl:template match="/">
  <xsl:for-each select="section">
    <xsl:call-template name="section">
      <xsl:with-param name="counter" select="{position()}"/>
    </xsl:call-template>
  </xsl:for-each>
</xsl:template>

<xsl:template name="section">
  <xsl:param name="counter"/>
  <span class="title" id="title-{$counter}">
    <xsl:value-of select="title"/>
  </span>
</xsl:template>
1
ответ дан 28 November 2019 в 04:42
поделиться

Haven't tried this myself, but you could try and pass a parameter to the template. В вашем первом шаблоне вы устанавливаете параметр count () (или может быть current ()?) В операторе for-each, а затем передаете это значение в свой шаблон "section".

Подробнее об передаче параметров в шаблоны

0
ответ дан 28 November 2019 в 04:42
поделиться

Variables in XSLT are immutable so you have to approact the problem with that in mind. You could either use position() directly:

<xsl:template match="/"> 
   <xsl:for-each select="section">
      <xsl:call-template name="section"/>
   </xsl:for-each>
</xsl:template>

<xsl:template name="section">
   <span class="title" id="title-{position()}"><xsl:value-of select="title"/></span>
</xsl:template>

Or in a more template orientated way:

<xsl:template match="/"> 
   <xsl:apply-templates select="section"/>
</xsl:template>

<xsl:template match="section">
   <span class="title" id="title-{position()}"><xsl:value-of select="title"/></span>
</xsl:template>
6
ответ дан 28 November 2019 в 04:42
поделиться
Другие вопросы по тегам:

Похожие вопросы: