XSLT 1.0 文本列表到单个元素和重复删除 [英] XSLT 1.0 textlist to individual elements and duplicate removal

查看:27
本文介绍了XSLT 1.0 文本列表到单个元素和重复删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XML 文档:

I have the following XML document:

<?xml version="1.0" encoding="UTF-8"?>
<cars>
    <car body="Wagon">
        <text>Red</text>
    </car>
    <car body="Sedan">
        <text>Yellow</text>
    </car>
    <car body="Sedan">
        <text></text>
    </car>
    <car body="Wagon">
        <textlist>
            <text>Red</text>
            <text>Green</text>
            <text>Black</text>
            <text>Blue</text>
        </textlist>
    </car>
    <car body="Sedan">
        <textlist>
            <text>Yellow</text>
            <text>Orange</text>
        </textlist>
    </car>
    <car body="Fastback">
        <textlist>
            <text>Yellow</text>
            <text>Red</text>
            <text>Green</text>
            <text>Black</text>
            <text>Blue</text>
        </textlist>
    </car>
    <car body="Fastback">
        <textlist>
            <text>Pink</text>
            <text>Red</text>
            <text>Orange</text>
        </textlist>
    </car>
</cars>

使用 XSLT 1.0 我需要将 XML 文档转换为这种格式:

Using XSLT 1.0 I need to transform the XML document to this format:

<?xml version="1.0" encoding="UTF-8"?>
<cars>
    <car type="Wagon">Red</car>
    <car type="Sedan">Yellow</car>
    <car type="Wagon">Green</car>
    <car type="Wagon">Black</car>
    <car type="Wagon">Blue</car>
    <car type="Sedan">Orange</car>
</cars>

请注意:

  1. body="Fastback" 被排除在外
  2. 排除重复项(红色货车出现两次)
  3. Textlist 多个项目被单独放置输出 XML 中的元素
  4. 忽略空值

推荐答案

这是一个示例:

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

<xsl:output indent="yes"/>

<xsl:key name="k1" 
  match="car[not(@body = 'Fastback')]//text"
  use="concat(ancestor::car/@body, '|', .)"/>

<xsl:template match="cars">
  <xsl:copy>
    <xsl:apply-templates select="car[not(@body =  'Fastback')]//text
      [generate-id() = generate-id(key('k1', concat(ancestor::car/@body, '|', .))[1])]"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="text">
  <car type="{ancestor::car/@body}">
    <xsl:value-of select="."/>
  </car>
</xsl:template>

</xsl:stylesheet>

它使用 Muechian 分组,参见 http://www.jenitennison.com/xslt/grouping/muenchian.xml 如果您不熟悉 XSLT 1.0 方法.

It uses Muechian grouping, see http://www.jenitennison.com/xslt/grouping/muenchian.xml if you are not familiar with that XSLT 1.0 approach.

这篇关于XSLT 1.0 文本列表到单个元素和重复删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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