我需要根据XML中的属性进行xslt转换。对于ed-Cover.i的所有属性,需要在目标xml中创建coverage标记,并将元素作为子元素映射到它。可以帮助我< / p>
输入XML:
<c>
<m p='ed-Cover'></m>
<m p='premum-amt' v='100'></m>
<m p='premium-rate' v='10'></m>
<m p='prem-date' v='10-04-15'></m>
<m p='ed-Cover'></m>
<m p='premum-amt' v='50'></m>
<m p='premium-rate' v='5'></m>
<m p='prem-date' v='12-03-15'></m>
<m p='ed-Cover'></m>
<m p='premum-amt' v='75'></m>
<m p='premium-rate' v='7'></m>
<m p='prem-date' v='3-05-15'></m>
</c>
XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<coverages>
<xsl:for-each select="c/m">
</xsl:for-each>
</coverages>
</xsl:template>
</xsl:stylesheet>
预期产出:
<coverages>
<coverage>
<coverage-prem-amt>100</coverage-prem-amt>
<coverage-prem-rate>10</coverage-prem-rate>
<coverage-prem-date>10-04-15</coverage-prem-date>
</coverage>
<coverage>
<coverage-prem-amt>50</coverage-prem-amt>
<coverage-prem-rate>5</coverage-prem-rate>
<coverage-prem-date>12-03-15</coverage-prem-date>
</coverage>
<coverage>
<coverage-prem-amt>75</coverage-prem-amt>
<coverage-prem-rate>7</coverage-prem-rate>
<coverage-prem-date>3-05-15</coverage-prem-date>
</coverage>
<coverages>
答案 0 :(得分:0)
如果您希望coverage
元素的m
元素带有“ed-Cover”作为属性值,则应将xsl:for-each
更改为此
<xsl:for-each select="c/m[@p='ed-Cover']">
要获取所需的值(这是此元素的兄弟),可能有助于拥有一个键,以便您可以根据它们之前的第一个“ed-Cover”查找其他元素
<xsl:key name="cover" match="m" use="generate-id(preceding-sibling::m[@p='ed-Cover'][1])" />
因此,要获得当前“ed-Cover”的保费金额,您可以这样做
<xsl:variable name="id" select="generate-id()" />
<coverage-prem-amt>
<xsl:value-of select="key('cover', $id)[@p='premum-amt']/@v" />
</coverage-prem-amt>
试试这个XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="cover" match="m" use="generate-id(preceding-sibling::m[@p='ed-Cover'][1])" />
<xsl:template match="/">
<coverages>
<xsl:for-each select="c/m[@p='ed-Cover']">
<xsl:variable name="id" select="generate-id()" />
<coverage>
<coverage-prem-amt>
<xsl:value-of select="key('cover', $id)[@p='premum-amt']/@v" />
</coverage-prem-amt>
<coverage-prem-rate>
<xsl:value-of select="key('cover', $id)[@p='premium-rate']/@v" />
</coverage-prem-rate>
<coverage-prem-date>
<xsl:value-of select="key('cover', $id)[@p='prem-date']/@v" />
</coverage-prem-date>
</coverage>
</xsl:for-each>
</coverages>
</xsl:template>
</xsl:stylesheet>
编辑:如果您不想在值不存在时创建元素,请尝试模板方法
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="cover" match="m" use="generate-id(preceding-sibling::m[@p='ed-Cover'][1])" />
<xsl:template match="/">
<coverages>
<xsl:for-each select="c/m[@p='ed-Cover']">
<xsl:variable name="id" select="generate-id()" />
<coverage>
<xsl:apply-templates select="key('cover', $id)[@p='premum-amt']" />
<xsl:apply-templates select="key('cover', $id)[@p='premium-rate']" />
<xsl:apply-templates select="key('cover', $id)[@p='prem-date']" />
</coverage>
</xsl:for-each>
</coverages>
</xsl:template>
<xsl:template match="m[@p='premum-amt']">
<coverage-prem-amt>
<xsl:value-of select="@v" />
</coverage-prem-amt>
</xsl:template>
<xsl:template match="m[@p='premium-rate']">
<coverage-prem-rate>
<xsl:value-of select="@v" />
</coverage-prem-rate>
</xsl:template>
<xsl:template match="m[@p='prem-date']">
<coverage-prem-date>
<xsl:value-of select="@v" />
</coverage-prem-date>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:0)
这也可行:
<xsl:template match="c">
<xsl:element name="coverages">
<xsl:apply-templates select="m[@p='premum-amt']"/>
</xsl:element>
</xsl:template>
<xsl:template match="m[@p='premum-amt']">
<xsl:element name="coverage">
<xsl:element name="coverage-prem-amt">
<xsl:value-of select="@v"/>
</xsl:element>
<xsl:element name="coverage-prem-rate">
<xsl:value-of select="following-sibling::m[@p='premium-rate'][1]/@v"/>
</xsl:element>
<xsl:element name="coverage-prem-date">
<xsl:value-of select="following-sibling::m[@p='prem-date'][1]/@v"/>
</xsl:element>
</xsl:element>
</xsl:template>