我有基本的查询,我有给定的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>
我开发的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: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>
<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="producttypes_template">
<xsl:with-param name="TradeHeaderVarr" select="$CMaccounttypeGroup/eur:ProductType" />
</xsl:call-template>
</ProductType>
<!-- <nameforproduct>
<xsl:value-of select="$CMaccounttypeGroup/eur:ProductType/@name" />
<xsl:call-template name="product_template">
<xsl:with-param name="TradeHeaderVarr" select="$CMaccounttypeGroup" />
</xsl:call-template>
</nameforproduct> -->
</EurexMessageObject>
</xsl:for-each>
</xsl:template>
<xsl:template name="producttypes_template">
<xsl:param name="TradeHeaderVarr" />
<xsl:for-each select="$TradeHeaderVarr">
<xsl:value-of select="eur:ProductType" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我希望转换后的最终 xml 应该是这样的,如下所示,其中有两个 p 的 enetry
'
<EurexMessageObject>
<name>A1</name>
<ProductType> swap
</ProductType>
</EurexMessageObject>
<EurexMessageObject>
<name>A2</name>
<ProductType> swap
</ProductType>
</EurexMessageObject>
<EurexMessageObject>
<name>A3</name>
<ProductType> swap
</ProductType>
</EurexMessageObject>
<EurexMessageObject>
<name>A4</name>
<ProductType> swap
</ProductType>
</EurexMessageObject>
<EurexMessageObject>
<name>A5</name>
<ProductType> swap
</ProductType>
</EurexMessageObject>
<EurexMessageObject>
<name>A6</name>
<ProductType> swap
</ProductType>
</EurexMessageObject>
<EurexMessageObject>
<name>A7</name>
<ProductType> swap
</ProductType>
</EurexMessageObject>
<EurexMessageObject>
<name>A8</name>
<ProductType> swap
</ProductType>
</EurexMessageObject>
<EurexMessageObject>
<name>A9</name>
<ProductType> swap
</ProductType>
</EurexMessageObject>
<EurexMessageObject>
<name>P</name>
<ProductType> swap
</ProductType>
</EurexMessageObject>
<EurexMessageObject>
<name>P</name>
<ProductType>fra
</ProductType>
</EurexMessageObject>
`
请告知需要在 xsl 中进行哪些更改才能实现最终的 xml,如上所示
以下是您可以相当简洁地执行此操作的方法:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:eur="http://www.eurexchange.com/EurexIRSFullInventoryReport">
<xsl:output method="text" />
<xsl:template match="eur:ProductType">
<xsl:variable name="acctTyp" select="../@name" />
<xsl:value-of select="concat($acctTyp,
substring(' ', string-length($acctTyp)),
@name, '
')" />
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
在示例输入上运行时,结果为:
A9 Swap
P Swap
P FRA
下面是一个 XSLT,用于生成上面显示的 XML 格式:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:eur="http://www.eurexchange.com/EurexIRSFullInventoryReport"
exclude-result-prefixes="eur">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:template match="/*">
<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>
在示例 XML 上运行时,结果为:
<Eurexflows>
<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>
试试这个:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:fpml="http://www.fpml.org/FpML-5/confirmation">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*:CM">
<xsl:for-each select="*:acctTypGrp/*:ProductType">
<xsl:value-of select="../@name"/>
<xsl:value-of select="substring(' ', string-length(../@name))"/>
<xsl:value-of select="@name"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>