在XSLT for-each循环中更新变量值 [英] Update variable value in XSLT for-each loop

查看:67
本文介绍了在XSLT for-each循环中更新变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个XML:

 <   Root  >  
< 员工 >
< 名称 > Dash < /姓名 >
< 年龄 > 23 < /年龄 >
< /员工 >
< 员工 >
< 名称 > Gwen < / Name >
< 年龄 > 22 < /年龄 >
< / Employee >
< / Root >



我需要转换使用XSLT到XML以下:

< pre lang =xml>< Root> 
< Employee>
< Name> Dash,Gwen< / Name>
< Age> 23,22< / Age>
< / Employee>
< / root
>



我正在使用for-each循环来获取<的每个子节点的值。雇员>我正面临的问题是我无法弄清楚如何将连接值存储在XSLT中的另一个临时变量中。我在许多网站上发现我们无法更新XSLT中的变量,所以有没有替代解决方案呢? div class =h2_lin>解决方案

不确定为什么要搞砸一个格式良好的XML文档并创建一些难以维护的东西,但我应该判断谁。



这是一个解决方案。

 <?  xml     version   =  1.0   编码  =  utf-8  >  
< xsl:stylesheet version = 1.0 xmlns:xsl = http://www.w3.org/1999/XSL/Transform

xmlns:msxsl = urn:schemas-microsoft-com:xslt exclude-result-prefixes = ms xsl

>
< xsl:output 方法 = xml 缩进 = / >

< xsl:template 匹配 = Root >
< Root >
< 员工 >
< 名称 >
< xsl :for-each 选择 = 员工 >
< ; xsl:value-of select = 名称 / >
< xsl:if test = follow-sibling :: * >
< xsl:text > < / xsl:text >
< / xsl:if >
< / xsl:for-each >
< /名称 >
< 年龄 >
< xsl:for-each 选择 = 员工 >
< xsl:value-of 选择 = 年龄 / >
< xsl:if test = follow-sibling: :* >
< xsl:text > < / xsl:text >
< / xsl:if >
< / xsl:for-each >
< /年龄 >
< /员工 >
< / Root >
< / xsl:template >

< / xsl:样式表 >


这就是我想到的:



我希望它对别人有帮助



< xsl:stylesheet version = 1.0
xmlns:xsl =http://www.w3.org/1999/XSL/Transform>
< xsl:output method =xmlversion =1.0encoding =UTF-8indent =yes/>

< xsl:template match =/ Root>
< Root>
< Employee>
< Name>
< xsl:apply-templates select =Employee / Name/>
< / Name>
<年龄>
< xsl:apply-templates select =Employee / Age/>
< / Age>
< / Employee>
< / Root>
< / xsl:template>

< xsl:template match =Name | Age>
< xsl:value-of select =。/>
< xsl:if test =position()!= last()>
< xsl:text>,< / xsl:text>
< / xsl:if>
< / xsl:template>

< / xsl:stylesheet>


I have this XML:

<Root>
  <Employee>
    <Name>Dash</Name>
    <Age>23</Age>
  </Employee>
  <Employee>
    <Name>Gwen</Name>
    <Age>22</Age>
  </Employee>
</Root>


And I need to convert to below XML using XSLT:

<pre lang="xml"><Root>
  <Employee>
    <Name>Dash,Gwen</Name>
    <Age>23,22</Age>
  </Employee>
</Root
>


I am using the for-each loop to get the values of the sub-nodes of the <employee> node.The problem I am facing is I cannot figure out how to store the concatenated values in another temperoary variable in XSLT.I found on many sites that we cannot update the variables in the XSLT, so is there any alternative solution for this?

解决方案

Not sure why you want to screw up a well formed XML document and create something that will be difficult to maintain, but who am I to judge.

Here is one solution.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"

>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="Root">
      <Root>
        <Employee>
          <Name>
            <xsl:for-each select="Employee">
              <xsl:value-of select="Name"/>
              <xsl:if test="following-sibling::*">
                <xsl:text>,</xsl:text>
              </xsl:if>
            </xsl:for-each>
          </Name>
          <Age>
            <xsl:for-each select="Employee">
              <xsl:value-of select="Age"/>
              <xsl:if test="following-sibling::*">
                <xsl:text>,</xsl:text>
              </xsl:if>
            </xsl:for-each>
          </Age>
        </Employee>
      </Root>
    </xsl:template>

</xsl:stylesheet>


This is what I figured out:

I hope it will be helpful to others

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

<xsl:template match="/Root">
    <Root>
        <Employee>
            <Name>
                <xsl:apply-templates select="Employee/Name"/>
            </Name>
            <Age>
                <xsl:apply-templates select="Employee/Age"/>
            </Age>
        </Employee>
    </Root>
</xsl:template>

<xsl:template match="Name|Age">
    <xsl:value-of select="."/>
    <xsl:if test="position()!=last()">
        <xsl:text>,</xsl:text>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>


这篇关于在XSLT for-each循环中更新变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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