1

这是我的 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>

正如你所理解的,我有这个问题:如果地图有孩子,我必须只应用模板,否则我会像上面的例子一样丢失节点。我尝试了不同的方法来仅匹配有孩子的地图,但我有两种地图:“myMap”,每个条目都有两个值,“dateMap”,每个条目都有一个值。

非常感谢您的帮助!

4

2 回答 2

2

与其contains()查看名称中是否包含“Map”,不如查看元素是否包含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>
于 2013-06-04T16:39:25.280 回答
1

添加一个模板match="*[contains(name(), 'Map')] [not(self::myMap)] [not(*)]"并让它处理没有子元素的地图元素的情况。

于 2013-06-04T16:19:41.763 回答