首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在xsl:for tag while xml转换中获取重复的值

在xsl:for tag while xml转换中获取重复的值
EN

Stack Overflow用户
提问于 2014-12-13 16:30:19
回答 1查看 100关注 0票数 0

我有下面的xml

代码语言:javascript
复制
 <?xml version="1.0" encoding="UTF-8"?>
<Report xmlns:fpml="http://www.fpml.org/FpML-5/confirmation" xmlns="http://www.eurexchange.com/EurexIRSFullInventoryReport" name="CB202 Full Inventory Report">
<reportNameGrp>
<CM>
<acctTypGrp name="A9">
<ProductType name="Swap">
</ProductType>
</acctTypGrp>
<acctTypGrp name="P">
<ProductType name="Swap">
</ProductType>
<ProductType name="FRA">
</ProductType>
</acctTypGrp>
</CM>
</reportNameGrp>
</Report>

为此,我编写了用于xml转换的xsl,我在下面编写了xslt。

代码语言:javascript
复制
<xsl:stylesheet version="1.0"  xmlns:fpml="http://www.fpml.org/FpML-5/confirmation"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:eur="http://www.eurexchange.com/EurexIRSFullInventoryReport" 
    xmlns:java="http://xml.apache.org/xslt/java"    exclude-result-prefixes="java">
     <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>


    <!-- Main Template starts from here -->
  <xsl:template match="/eur:Report">
    <Eurexflows>
    <xsl:call-template name="EurexreportNameGrp_block">
    <xsl:with-param name="CMaccounttypeGroup" select="/eur:Report/eur:reportNameGrp/eur:CM/eur:acctTypGrp" />
    </xsl:call-template>
    </Eurexflows>
    </xsl:template>

    <!-- Main tempalte ends --> 

    <!-- sub templates starts -->     
   <xsl:template name="EurexreportNameGrp_block">
    <xsl:param name="CMaccounttypeGroup" />
    <xsl:for-each select="$CMaccounttypeGroup">
      <EurexMessageObject>
     <name>
        <xsl:value-of select="@name" />
     </name>
     <ProductType>
     <xsl:value-of select="$CMaccounttypeGroup/eur:ProductType/@name" />
     <xsl:call-template name="generateData">
            <xsl:with-param name="data" select="."/>
          </xsl:call-template>
     </ProductType>
     </EurexMessageObject>
</xsl:for-each>
</xsl:template>
<xsl:template name="generateData">
        <xsl:param name="data" />
        <xsl:for-each select="$data/eur:ProductType">
            <xsl:value-of select="./@name" />
    </xsl:for-each>
    </xsl:template> 
    </xsl:stylesheet>

但是经过xsl转换后,我得到的xml格式如下:

代码语言:javascript
复制
<Eurexflows xmlns:eur="http://www.eurexchange.com/EurexIRSFullInventoryReport"
    xmlns:fpml="http://www.fpml.org/FpML-5/confirmation">
    <EurexMessageObject>
        <name>A9</name>
        <ProductType>SwapSwap</ProductType>
    </EurexMessageObject>
    <EurexMessageObject>
        <name>P</name>
        <ProductType>SwapSwapFRA</ProductType>
    </EurexMessageObject>
</Eurexflows>

但是我想要这种格式的xml,

代码语言:javascript
复制
<Eurexflows xmlns:eur="http://www.eurexchange.com/EurexIRSFullInventoryReport"
    xmlns:fpml="http://www.fpml.org/FpML-5/confirmation">
    <EurexMessageObject>
        <name>A9</name>
        <ProductType>Swap</ProductType>
    </EurexMessageObject>
    <EurexMessageObject>
        <name>P</name>
        <ProductType>Swap</ProductType>
    </EurexMessageObject>
    <EurexMessageObject>
        <name>P</name>
        <ProductType>FRA</ProductType>
    </EurexMessageObject>
</Eurexflows>

请保存转换后如何实现上述xml,并请告知我需要在xsl中做哪些必要的更改,特别是在我的generateData template..please advise中。

EN

回答 1

Stack Overflow用户

发布于 2014-12-13 20:06:52

您使XSLT变得不必要地长。你也可以这样做:

代码语言:javascript
复制
<xsl:stylesheet version="1.0"  xmlns:fpml="http://www.fpml.org/FpML-5/confirmation" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:eur="http://www.eurexchange.com/EurexIRSFullInventoryReport">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/eur:Report">
    <Eurexflows>
        <xsl:apply-templates select="eur:reportNameGrp/eur:CM/eur:acctTypGrp/eur:ProductType"/>
    </Eurexflows>
</xsl:template>
<xsl:template match="eur:ProductType">
    <EurexMessageObject>
        <name>
            <xsl:value-of select="../@name"/>
        </name>
        <ProductType>
            <xsl:value-of select="@name"/>
        </ProductType>
    </EurexMessageObject>
</xsl:template>
</xsl:stylesheet>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27456972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档