Xslt 2 将非唯一元素转换为网格的属性 [英] Xslt 2 convert non-unique elements to attributes for grid

查看:20
本文介绍了Xslt 2 将非唯一元素转换为网格的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有这种结构的 XML 文件

I have an XML file with this structure

<levels>
  <level id="0" qd="NE">
    <gate>99</gate>
    <zone>2</zone>
    <laydown>4</laydown>
  </level>
  <level id="0" qd="SE">
    <gate>1</gate>
    <zone>6</zone>
    <laydown>1</laydown>
    <laydown>2</laydown>
    <laydown>3</laydown>
    <zone>5</zone>
    <zone>5</zone>
  </level>
</levels>

我需要把它转换成这样的东西才能在网格中显示

And I need to convert it to something like this to display in a grid

<level id="0" qd="NE" gate="99" zone="2"  laydown="3">
<level id="0" qd="NE" gate="1" zone="5,6" laydown="1,2,3">

我发现 xslt 有效,但在元素不唯一的情况下无效.他们只选择最后一个元素,表明每个元素都覆盖了前一个元素.没有包括它,因为我宁愿看到正确的解决方案,也不愿看到我刚开始工作的笨拙尝试.我是 xsl/xslt 新手,因此任何注释都会有所帮助.我使用 vs 2013

I have found xslt that works but not where the elements are non-unique. They only pick up the LAST element suggesting that each element is overwriting the previous. Have not included it as I would rather see the proper solution than my bodged attempt just made to work. I am an xsl/xslt newbie so any annotation will be helpful. I work with vs 2013

推荐答案

如果您为每个元素创建一个属性,它肯定会覆盖您之前创建的任何属性(具有相同名称).

If you're creating an attribute for each element, it will definitely overwrite any previous attribute that you've created (with the same name).

您将其标记为 XSLT 2.0,因此这里有一个 2.0 选项.输出与您的不完全匹配;你的例子在几个地方没有多大意义.(比如为什么第一个 levellaydown="3" 而不是 laydown="4"level> 有 qd="NE".)

You tagged this as XSLT 2.0, so here's a 2.0 option. The output doesn't match yours exactly; your example doesn't make a whole lot of sense in a couple of places. (Like why does the first level have laydown="3" instead of laydown="4" and both level's have qd="NE".)

XML 输入

<levels>
    <level id="0" qd="NE">
        <gate>99</gate>
        <zone>2</zone>
        <laydown>4</laydown>
    </level>
    <level id="0" qd="SE">
        <gate>1</gate>
        <zone>6</zone>
        <laydown>1</laydown>
        <laydown>2</laydown>
        <laydown>3</laydown>
        <zone>5</zone>
        <zone>5</zone>
    </level>
</levels>

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:local="local" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:function name="local:sort">
        <xsl:param name="seq"/>
        <xsl:perform-sort select="$seq">
            <xsl:sort data-type="number"/>
        </xsl:perform-sort>
    </xsl:function>

    <xsl:template match="level">
        <xsl:variable name="level" select="."/>
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:for-each select="distinct-values(*/name())">
                <xsl:attribute name="{.}" 
                    select="string-join(distinct-values(local:sort($level/*[name()=current()])),',')"/>
            </xsl:for-each>
        </xsl:copy>        
    </xsl:template>

</xsl:stylesheet>

输出

<levels>
   <level id="0" qd="NE" gate="99" zone="2" laydown="4"/>
   <level id="0" qd="SE" gate="1" zone="5,6" laydown="1,2,3"/>
</levels>

这篇关于Xslt 2 将非唯一元素转换为网格的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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