简体   繁体   English

如何在xsl中为变量或参数设置新值

[英]how to set a new value to variable or param in xsl

I need some help about xsl. 我需要一些关于xsl的帮助。 I have a date attribute in source xml and i need to know its previous value because i need categorized according to date. 我在源xml中有一个日期属性,我需要知道它以前的值,因为我需要根据日期进行分类。 here is the source xml. 这是源xml。

source xml: source xml:

<root>
  <license id="a" expireDate="2010-02-01"/>
  <license id="b" expireDate="2010-02-01"/>
  <license id="c" expireDate="2010-02-01"/>
  <license id="d" expireDate="2010-02-04"/>
  <license id="e" expireDate="2010-02-04"/>
  <license id="f" expireDate="2010-02-12"/>
  <license id="g" expireDate="2010-02-12"/>
</root>

I need to transform this to 我需要将其转换为

<licenses>
   <expDate value="2010-02-01">
      <license>a</license>
      <license>b</license>
      <license>c</license>
   </expDate>
   <expDate value="2010-02-04">
      <license>d</license>
      <license>e</license>
   </expDate>
   <expDate value="2010-02-12">
      <license>f</license>
      <license>g</license>
   </expDate>
</licenses>

Actually, i can transform source xml which have different format from the given one. 实际上,我可以转换源xml,它具有与给定格式不同的格式。 I have read some articles but could not find the way to do this. 我读过一些文章,却找不到办法。 How can I keep the previous date value and check if its different from current one. 如何保留上一个日期值并检查它是否与当前日期值不同。

Thanks 谢谢

You can't change the value of a variable in XSLT, but you don't need to. 您无法在XSLT中更改变量的值,但您不需要。 The following stylesheet: 以下样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="byDate" match="license" use="@expireDate" />
    <xsl:template match="/">
        <licenses>
            <xsl:apply-templates />
        </licenses>
    </xsl:template>
    <xsl:template
        match="license[generate-id() = 
                       generate-id(key('byDate', @expireDate)[1])]">
        <expDate value="{@expireDate}">
            <xsl:apply-templates select="key('byDate', @expireDate)"
                mode="group" />
        </expDate>
    </xsl:template>
    <xsl:template match="license" mode="group">
        <license>
            <xsl:value-of select="@id" />
        </license>
    </xsl:template>
    <xsl:template match="license" />
</xsl:stylesheet>

Applied to this input: 适用于此输入:

<root>
  <license id="a" expireDate="2010-02-01"/>
  <license id="b" expireDate="2010-02-01"/>
  <license id="c" expireDate="2010-02-01"/>
  <license id="d" expireDate="2010-02-04"/>
  <license id="e" expireDate="2010-02-04"/>
  <license id="f" expireDate="2010-02-12"/>
  <license id="g" expireDate="2010-02-12"/>
</root>

Produces the following output: 产生以下输出:

<licenses>
    <expDate value="2010-02-01">
        <license>a</license>
        <license>b</license>
        <license>c</license>
    </expDate>
    <expDate value="2010-02-04">
        <license>d</license>
        <license>e</license>
    </expDate>
    <expDate value="2010-02-12">
        <license>f</license>
        <license>g</license>
    </expDate>
</licenses>

Note: Your original XML was not well-formed. 注意:您的原始XML格式不正确。 I moved the license value to an id attribute. 我将许可证值移动到id属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM