XSLT 中的序列 [英] sequences in XSLT

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

问题描述

我的 xml 输入是-

使用 xslt 输出应该是

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

输出应该在父元素中包含元素序列.混合元素序列将移动到父节点s"内.是否有任何 xslt 命令可以检测序列并相应地处理 xml.非常感谢.

解决方案

这里,使用名称不同的第一个前面的兄弟,以便对记录进行分组:

<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/><xsl:key name="adjacentByName" match="foo/*" use="generate-id(preceding-sibling::*[not(name()=name(current()))][1])"/><xsl:template match="/"><foo><s><xsl:for-each select="foo/*[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:模板></xsl:stylesheet>

<小时>

添加:

<块引用>

但是我得到的输出与我想要的输出不同...

应用于您的(已清理)输入时:

结果是:

在我看来,这正是您要求的结果:

除了 <foobar> 元素中的 > 字符,我已经从输入中删除了这些字符.

--

附注请不要在回答后删除您的问题.

my xml input is-

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

Output using xslt should be

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

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

the output should to have sequence-of elements inside a parent. Mixed sequence-of elements will be moved inside parents node "s". Is there any xslt command that detects the sequence and process the xml accordingly.Many thanks.

解决方案

Same as here, use the id of the first preceding sibling whose name is different in order to group the records:

<?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="foo/*" use="generate-id(preceding-sibling::*[not(name()=name(current()))][1])" />

<xsl:template match="/">
<foo><s>
    <xsl:for-each select="foo/*[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>


Added:

But the output i am getting is not same as my desired output...

When applied to your (cleaned up) input of:

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

the result is:

<?xml version="1.0" encoding="utf-8"?>
<foo>
  <s>
    <s>
      <bar>bar</bar>
      <bar>bar</bar>
    </s>
    <s>
      <foobar>foobar</foobar>
      <foobar>foobar</foobar>
      <foobar>foobar</foobar>
    </s>
    <s>
      <bar>bar</bar>
      <bar>bar</bar>
    </s>
  </s>
</foo>

which to me seems to be exactly the result you have asked for:

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

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

except for the > characters inside the <foobar> elements, which I have removed from the input.

--

P.S. Please DO NOT delete your question after it has been answered.

这篇关于XSLT 中的序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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