简体   繁体   English

xml转换时在xsl:for标签中获取重复值

[英]getting duplicated value in xsl:for tag while xml transformation

I have the below xml 我有下面的xml

 <?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>

and for which i have written the xsl for xml transformation , I have written the xslt below 而我已经为XML转换编写了xsl,我在下面编写了xslt

<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>

but upon xsl transformation I am getting the xml in below format 但是在xsl转换后,我得到以下格式的xml

<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>

but I want xml in this format, 但我想要这种格式的xml,

<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>

please avise how to achieve the above xml after transformation and also please advise what necessary changes i need to do in xsl specially in my generateData template..please advise folks 请建议转换后如何实现上述xml,还请特别在我的generateData模板中告知我需要在xsl中做哪些必要的更改。

You've made the XSLT unnecessarily long. 您使XSLT不必要地变长了。 You can do it this way too: 您也可以通过以下方式进行操作:

<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>

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

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