如何使用 XSLT 将这个子元素组合在一起? [英] How to combine this child element together using XSLT?

查看:22
本文介绍了如何使用 XSLT 将这个子元素组合在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这个输入:

<root> 
    <library id="L1">
        <shelf1 id="1">
            <book id="1" category="science">
                <attributes>                    
                    <year>2000</year>
                </attributes>
                <attributes>
                    <author>xx</author>
                    <year>2010</year>
                    <isbn>001</isbn>
                </attributes>
                <attributes>
                    <author>yy</author>
                    <publisher>zz</publisher>
                    <isbn>002</isbn>
                </attributes>
                <other>y</other>
            </book>  
            <book id="2" category="science">
                ...
            </book>
        </shelf1>

        <shelf2>...</shelf2>
    </library>
</root>

预期输出:

<root> 
    <library id="L1">
        <shelf1 id="1">
            <book id="1" category="science">
                <attributes>
                    <author>yy</author>                    
                    <publisher>zz</publisher>
                    <isbn>002</isbn>
                    <year>2010</year>
                </attributes>
                <other>y</other>
            </book>  
            <book id="2" category="science">
                ...
            </book>
        </shelf1>

        <shelf2>...</shelf2>
    </library>
</root>

我需要将元素属性"组合在一起.如果存在两个或多个属性,当属性的子级之前不存在时,我们将子级保留为新信息,但如果之前存在,我们只使用最新值.

I need to combine the element 'attributes' together. If two or more attributes exist, when the child of the attributes never exist before, we keep the child as new information, but if it exists previously we simply use the latest value.

如何在 XSLT 1.0 或 2.0 中进行此类转换?非常感谢您的帮助.

How to do such transformation in XSLT 1.0 or 2.0?Thanks very much for the help.

约翰

推荐答案

这种转换 (XSLT 2.0):

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="book[attributes]">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <attributes>
      <xsl:for-each-group select="attributes/*" group-by="name()">
        <xsl:sort select="current-grouping-key()"/>
        <xsl:apply-templates select="."/>
      </xsl:for-each-group>
    </attributes>
    <xsl:apply-templates select="*[not(self::attributes)]"/>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<root>
    <library id="L1">
        <shelf1 id="1">
            <book id="1" category="science">
                <attributes>
                    <year>2000</year>
                </attributes>
                <attributes>
                    <author>xx</author>
                    <year>2010</year>
                    <isbn>001</isbn>
                </attributes>
                <attributes>
                    <author>yy</author>
                    <publisher>zz</publisher>
                    <isbn>002</isbn>
                </attributes>
                <other>y</other>
            </book>
            <book id="2" category="science">
            ...
            </book>
        </shelf1>
        <shelf2>...</shelf2>
    </library>
</root>

产生想要的、正确的结果:

<root>
      <library id="L1">
            <shelf1 id="1">
                  <book id="1" category="science">
            <attributes>
               <author>xx</author>
               <isbn>001</isbn>
               <publisher>zz</publisher>
               <year>2000</year>
            </attributes>
            <other>y</other>
         </book>
                  <book id="2" category="science">
            ...
            </book>
            </shelf1>
            <shelf2>...</shelf2>
      </library>
</root>

说明:

  1. 正确使用和覆盖身份规则.

  1. Proper use and overriding of the identity rule.

正确使用 XSLT 2.0 指令 xsl:for-each-group 和属性 group-by.

Proper use of the XSLT 2.0 instruction xsl:for-each-group with attribute group-by.

正确使用 XSLT 2.0 函数 current-grouping-key() 和 XPath 函数 name().

Proper use of the XSLT 2.0 function current-grouping-key() and the XPath function name().

这篇关于如何使用 XSLT 将这个子元素组合在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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