Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml XSL仅将模板应用于具有子节点的节点_Xml_Xslt - Fatal编程技术网

Xml XSL仅将模板应用于具有子节点的节点

Xml XSL仅将模板应用于具有子节点的节点,xml,xslt,Xml,Xslt,这是我的xml输入文件 <ELEMENTROOT> <id>10036</id> <firstName>Marco</firstName> <lastName>Nato</lastName> <addressSet> <address> <country> <displayValue>France<

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

10036
马可
北约
法国
生日
1973-11-29T00:00:00
性别
1042
女性
我想得到的结果

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

10036
马可
北约
法国
1973-11-29T00:00:00
1042
女性
我尝试的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”每个条目有一个值


非常感谢你的帮助

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

不要执行
contains()
来查看名称中是否有“Map”,检查元素是否有
条目/键
。然后您就可以“展开”
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>
<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>

10036
马可
北约
法国
生日
1973-11-29T00:00:00
性别
1042
女性
XSLT1.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/>
    <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/>
   <birthDate>1973-11-29T00:00:00</birthDate>
   <gender>
      <id>1042</id>
      <displayValue>Femminile</displayValue>
   </gender>
</ELEMENTROOT>

10036
马可
北约
法国
1973-11-29T00:00:00
1042
女性

您能否更详细地解释标记?您不理解表达式的哪一部分?我很高兴回答一些问题,这些问题表明您已经做出了一些努力来理解正在发生的事情。您编写的匹配标记的表达式,其Map为word和
[not(self::myMap)][not(*)]
?你能像Daniel Haley那样给我看看你的解决方案吗?