嵌套元素的Muenchian方法 [英] Muenchian method for nested elements

查看:91
本文介绍了嵌套元素的Muenchian方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的xml输入是-

<?xml version="1.0" encoding="UTF-8"?> 
<foo>  <bar>bar</bar> 
       <bar>bar</bar> 
       <foobar><baz>baz</baz></foobar> 
       <foobar>foobar</foobar>
       <bar>bar</bar>
       <bar>bar</bar> 
</foo>

使用xslt的输出应该是

Output using xslt should be

 <?xml version="1.0" encoding="UTF-8"?>

 <foo>  
 <s> 
 <s> 
 <bar>bar</bar>  
 <bar>bar</bar>
 </s>
 <s> 
 <foobar><baz>baz</baz></foobar>
 <foobar>foobar></foobar>
 </s> 
 <s>      
 <bar>bar</bar>  
 <bar>bar</bar> 
 </s>
 </s>
</foo>

输出应在父级内部具有sequence-of元素.混合顺序元素将在父节点"s"内移动.使用的xslt文件是-

the output should have sequence-of elements inside a parent. Mixed sequence-of elements will be moved inside parents node "s". the xslt file that is used is-

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:key name="adjacentByName" match="*/*" use="generate-id(preceding-sibling::*[not(name()=name(current()))][1])" />

<xsl:template match="/*">
<foo><s>
<xsl:for-each select="*[generate-id()=generate-id(key('adjacentByName', generate-id(preceding-sibling::*[not(name()=name(current()))][1]))[1])]">
    <s>
        <xsl:for-each select="key('adjacentByName', generate-id(preceding-sibling::*[not(name()=name(current()))][1]))">
            <xsl:copy-of select="."/>
        </xsl:for-each>
    </s>
</xsl:for-each>
</s></foo>
</xsl:template>

</xsl:stylesheet>

问题在于输出baz元素(第一个foobar元素的子元素)同时具有bar和foobar序列.

the problem is in output baz element (child element of first foobar element) is with both bar and foobar sequences.

<foo>
<s>
    <s>
        <bar>bar</bar>
        <bar>bar</bar>
        **<baz>baz</baz>**
    </s>
    <s>
        <foobar>
            <baz>baz</baz>
        </foobar>
        <foobar>foobar</foobar>
    </s>
    <s>
        <bar>bar</bar>
        <bar>bar</bar>
    </s>
</s>
</foo>

如何从条形序列中删除baz元素.非常感谢.

How can i remove baz element from bar sequences. Many thanks.

推荐答案

如何

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>

  <xsl:key name="kGroupLeader" match="*" use="
    generate-id(self::*[name() != name(preceding-sibling::*[1])])
  " />

  <xsl:template match="foo">
    <xsl:copy>
      <s>
        <xsl:for-each select="*[key('kGroupLeader', generate-id())]">
          <s>
            <xsl:copy-of select=". | following-sibling::*[
              name() = name(current())
              and
              generate-id(current()) = generate-id(
                preceding-sibling::*[key('kGroupLeader', generate-id())][1]
              )
            ]" />
          </s>
        </xsl:for-each>
      </s>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

输入

<foo>
  <bar id="1">bar</bar> 
  <bar id="2">bar</bar> 
  <foobar id="3"><baz>baz</baz></foobar> 
  <foobar id="4">foobar</foobar>
  <bar id="5">bar</bar>
  <bar id="6">bar</bar> 
</foo>

这产生

<foo>
  <s>
    <s>
      <bar id="1">bar</bar>
      <bar id="2">bar</bar>
    </s>
    <s>
      <foobar id="3">
        <baz>baz</baz>
      </foobar>
      <foobar id="4">foobar</foobar>
    </s>
    <s>
      <bar id="5">bar</bar>
      <bar id="6">bar</bar>
    </s>
  </s>
</foo>

这篇关于嵌套元素的Muenchian方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆