如何使用xslt重复某些xml的转换 [英] How can I repeat a transformation of some xml using xslt

查看:86
本文介绍了如何使用xslt重复某些xml的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想了解如何根据出现值将xslt提升到能够在节点上循环x次的下一个级别.

Wanting to find out how I can take this xslt to the next level of being able to loop around a node x number of times, based on the occurs value.

<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <Destination>acme.com</Destination>
    <Record>
       <FirstField length="10">AAAA</FirstField>
       <SecondField length="15">BBBB</SecondField>
       <SubRecord occurs="10">
          <ThirdField length="20">CCCC</ThirdField>
          <FourthField length="8">DDDD</FourthField>
       </SubRecord>
    </Record>
</Root>

现有的xslt看起来像这样

The existing xslt looks like this

<xsl:variable name="spaces" select="'                                   '"/>
   <xsl:template match="/">
       <xsl:text>"</xsl:text>
       <xsl:for-each select="//*[@length]">
           <xsl:variable name="spacelength" select="@length - string-length(.)"/>
           <xsl:value-of select="concat(substring($spaces, 1, $spacelength), .)"/>
       </xsl:for-each>
       <xsl:text>"</xsl:text>
   </xsl:template>

所需的输出是这样的.

"      AAAA           BBBB                CCCC    DDDD                CCCC    DDDD                CCCC    DDDD                CCCC    DDDD                CCCC    DDDD                CCCC    DDDD                CCCC    DDDD                CCCC    DDDD                CCCC    DDDD                CCCC    DDDD"

因此,正如您所看到的,当我看到其父节点包含一个出现值时,我想重复该变换,即应该在该特定节点周围循环.元素名称仍然是动态的,因此不能依赖于静态名称.仅有的两件事是长度&的属性名称:发生.

So as you can see, I am wanting to repeat the transform when it sees that its parent node contains an occurs value, ie so should loop around that particular node. Element names are still dynamic so can't depend on a static name for this. The only 2 things static are the attribute names of length & occurs.

推荐答案

也许是这样.

 <xsl:variable name="spaces" select="'                                   '"/>
 
 <xsl:template match="*[boolean(@occurs)]">
    <xsl:param name="currentOccurs"/>

    <xsl:if test="$currentOccurs = '' or number($currentOccurs) &gt; 0">
      <xsl:apply-templates select="node()|@*"/>
      <xsl:apply-templates select=".">
        <xsl:with-param name="currentOccurs">
          <xsl:choose>
            <xsl:when test="$currentOccurs = ''">
              <xsl:value-of select="number(@occurs) - 1"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="number($currentOccurs) - 1"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
      </xsl:apply-templates>
    </xsl:if>  
  </xsl:template>

  <xsl:template match="*[boolean(@length)]">
    <xsl:variable name="spacelength" select="@length - string-length(.)"/>
    <xsl:value-of select="concat(substring($spaces, 1, $spacelength), .)"/>
  </xsl:template>

  <xsl:template match="node()|@*">
    <xsl:param name="currentOccurs"/>
    <xsl:apply-templates select="node()|@*">
      <xsl:with-param name="currentOccurs" select="$currentOccurs"/>
    </xsl:apply-templates>
  </xsl:template>

这篇关于如何使用xslt重复某些xml的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆