这是我的xml输入文件
<ELEMENTROOT>
<id>10036</id>
<firstName>Marco</firstName>
<lastName>Nato</lastName>
<addressSet>
<address>
<country>
<displayValue>France</displayValue>
</country>
</address>
</addressSet>
<clobMap/>
<dateMap>
<entry>
<key>birthDate</key>
<value>1973-11-29T00:00:00</value>
</entry>
</dateMap>
<myMap>
<entry>
<key>gender</key>
<value>
<id>1042</id>
<displayValue>Femminile</displayValue>
</value>
</entry>
<myMap>
</ELEMENTROOT>
结果我想得到
<ELEMENTROOT>
<id>10036</id>
<firstName>Marco</firstName>
<lastName>Nato</lastName>
<addressSet>
<address>
<country>
<displayValue>France</displayValue>
</country>
</address>
</addressSet>
<clobMap/> <!-- Unlikely I don't have this with my xsl-->
<birthDate>
1973-11-29T00:00:00
</birthDate>
<gender>
<id>1042</id>
<displayValue>Femminile</displayValue>
</gender>
</ELEMENTROOT>
我尝试的xsl文件是:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="myMap">
<xsl:for-each select="entry">
<xsl:element name="{key}">
<xsl:copy-of select="value/*" />
</xsl:element>
</xsl:for-each>
</xsl:template>
<xsl:template match="*[contains(name(), 'Map')][not(contains(name(), 'myMap'))]">
<xsl:for-each select="./entry">
<xsl:element name="{key}">
<xsl:value-of select="value" />
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
你可以理解我有这个问题: 如果地图有子节点,我必须只应用模板,否则我会丢失节点,如上例所示。 我尝试了不同的方法来匹配只有孩子的Map,但我有两种地图:“myMap”,每个条目有两个值,“dateMap”,每个条目有一个值。
非常感谢你的帮助!
答案 0 :(得分:2)
不要使用contains()
来查看名称中是否包含“地图”,而是检查元素是否包含entry/key
。然后你可以“解开”value
。
示例...
XML输入
<ELEMENTROOT>
<id>10036</id>
<firstName>Marco</firstName>
<lastName>Nato</lastName>
<addressSet>
<address>
<country>
<displayValue>France</displayValue>
</country>
</address>
</addressSet>
<clobMap/>
<dateMap>
<entry>
<key>birthDate</key>
<value>1973-11-29T00:00:00</value>
</entry>
</dateMap>
<myMap>
<entry>
<key>gender</key>
<value>
<id>1042</id>
<displayValue>Femminile</displayValue>
</value>
</entry>
</myMap>
</ELEMENTROOT>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<!--Identity transform. Anything not matched by another template
will be output without change.-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!--Match any element that has an "entry" element that has a "key"
element.-->
<xsl:template match="*[entry/key]">
<!--Don't output anything. Only apply-templates to the "key" element.
We don't need "entry" for anything.-->
<xsl:apply-templates select="entry/key"/>
</xsl:template>
<!--Match "key" element.-->
<xsl:template match="key">
<!--Create a new element with the name based on the content
of "key".-->
<xsl:element name="{.}">
<!--Inside of the newly created element apply-templates to
the following sibling "value" element.-->
<xsl:apply-templates select="following-sibling::value"/>
</xsl:element>
</xsl:template>
<!--Match "value" element.-->
<xsl:template match="value">
<!--Don't output the "value" element itself. The apply-templates
will apply to any child node. The identity transform will handle
the child nodes. If the child is text(), it will output the text.
If the child/children are elements, it will output the elements.-->
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
<强>输出强>
<ELEMENTROOT>
<id>10036</id>
<firstName>Marco</firstName>
<lastName>Nato</lastName>
<addressSet>
<address>
<country>
<displayValue>France</displayValue>
</country>
</address>
</addressSet>
<clobMap/>
<birthDate>1973-11-29T00:00:00</birthDate>
<gender>
<id>1042</id>
<displayValue>Femminile</displayValue>
</gender>
</ELEMENTROOT>
答案 1 :(得分:1)
使用match="*[contains(name(), 'Map')] [not(self::myMap)] [not(*)]"
添加模板,让它处理没有子项的地图元素的情况。