【问题标题】:xslt transformation and limitingxslt 转换和限制
【发布时间】:2014-09-17 03:09:47
【问题描述】:

我有一个这样的 XML

<Ozellik isim="Renk-Beden">STANDART STD</Ozellik>
<Ozellik isim="Grup">ADMIN</Ozellik>
<Ozellik isim="Amac">DENEME</Ozellik>
<Ozellik isim="BlaBla">BLABLA</Ozellik>

并希望将其转换为 this 并限制为三个元素。如果超过 3 条记录,取前 3 条记录

<property1 name="Renk-Beden">STANDART STD</property1>
<property2 name="Grup">ADMIN</property2>
<property3 name="Amac">DENEME</property3>

我尝试了许多 xslt 代码,但无法将其转换为所需的输出。谢谢你的帮助。

我最后一次成功的尝试是:

<xsl:template name="loop">
    <xsl:param name="pCount"/>
    <xsl:param name="pValue"/>
    <xsl:param name="pAtt"/>
    <xsl:element name="property{$pCount}">
    <xsl:attribute name="name">
      <xsl:value-of select="$pAtt" />
    </xsl:attribute>
      <xsl:value-of select="$pValue" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="Ozellik">
    <xsl:param name="pCount" select="0"/>
    <xsl:for-each select="catalog/cd">
      <xsl:call-template name="loop">
       <xsl:with-param name="pCount" select="position()" />
       <xsl:with-param name="pValue" select="." />
       <xsl:with-param name="pAtt" select="@isim" />
       </xsl:call-template>
    </xsl:for-each>
  </xsl:template>

【问题讨论】:

  • I tried many xslt codes,你能发布你的最后一次尝试吗?
  • 我添加了我的最后一次尝试
  • 你为什么选择catalog/cd,你提供的样本输入中没有元素catalogcd

标签: xml xslt xslt-1.0


【解决方案1】:

使用

<xsl:template match="/">
  <xsl:apply-templates select="(//Ozellik)[position() &lt; 4]"/>
</xsl:template>

<xsl:template match="Ozellik">
  <xsl:element name="property{position()}">
    <xsl:apply-templates select="@* | node()"/>
  </xsl:element>
</xsl:template>

<xsl:template match="Ozellik/@isim">
  <xsl:attribute name="name">
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:template>

【讨论】:

  • 谢谢你这个工作,下面的答案也工作:) 应该标记哪一个?
【解决方案2】:

修复你的最后一次尝试。这是您需要做的。

删除for-each 元素

<xsl:for-each select="catalog/cd">
   <!-- existing inner elements -->
</xsl:for-each>

并将其替换为if 元素。

<xsl:if test="position() &lt; 4">
   <!-- existing inner elements -->
</xsl:if>

您的“Ozellik”模板将如下所示。

<xsl:template match="Ozellik">
  <xsl:param name="pCount" select="0"/>
  <xsl:if test="position() &lt; 4">
    <xsl:call-template name="loop">
      <xsl:with-param name="pCount" select="position()" />
      <xsl:with-param name="pValue" select="." />
      <xsl:with-param name="pAtt" select="@isim" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-22
    • 2017-04-12
    • 1970-01-01
    • 2023-04-07
    • 2021-09-05
    • 2011-09-17
    • 2012-07-18
    相关资源
    最近更新 更多