XSLT 1.0用于遍历带有逗号分隔值的多个XML元素

3
我有一个XML文档,结构如下:
<items>
 <item>
  <name>item1</name>
  <attributes>a,b,c,d</attributes>
 </item>
 <item>
  <name>item2</name>
  <attributes>c,d,e</attributes>
 </item>
</items>

对于每个独特的属性值(由逗号分隔),我需要列出与该值相关联的所有项目名称,如下所示:
a : item1
b : item1
c : item1, item2
d : item1, item2
e : item2

我的最初计划是使用模板将属性解析为属性节点,用适当的标签将每个节点包围起来,然后使用XPATH表达式将唯一值分离出来,如下所示:

Attribute[not(.=following::Attribute)]

但是由于模板的结果不是节点集,因此永远不会通过XML解析器,我无法遍历它。我还尝试了exslt的node-set()函数,只是意识到它也不允许我遍历单个属性节点。

目前我对简单的解决方法感到困惑,并且真的很感谢任何帮助或关于如何继续的想法。谢谢!


好问题,+1。请查看我的答案,其中包含完整的解决方案和解释。 - Dimitre Novatchev
我很喜欢思考这个问题。 - Wayne
2个回答

1

这个转换:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:msxsl="urn:schemas-microsoft-com:xslt"
 xmlns:ext="http://exslt.org/common">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kAtrByVal" match="attr" use="."/>

 <xsl:template match="/">
  <xsl:variable name="vrtfPass1">
   <groups>
    <xsl:apply-templates/>
   </groups>
  </xsl:variable>

  <xsl:variable name="vPass1"
       select="ext:node-set($vrtfPass1)"/>

  <xsl:apply-templates select="$vPass1/*"/>
 </xsl:template>

 <xsl:template match="item">
  <group name="{name}">
   <xsl:apply-templates select="attributes"/>
  </group>
 </xsl:template>

 <xsl:template match="attributes" name="tokenize">
  <xsl:param name="pText" select="."/>

  <xsl:if test="string-length($pText)">
   <xsl:variable name="vText" select=
        "concat($pText,',')"/>
   <attr>
    <xsl:value-of select="substring-before($vText,',')"/>
   </attr>
   <xsl:call-template name="tokenize">
    <xsl:with-param name="pText" select=
    "substring-after($pText,',')"/>
   </xsl:call-template>
  </xsl:if>
 </xsl:template>

 <xsl:template match=
  "attr[generate-id()
       =
        generate-id(key('kAtrByVal',.)[1])
       ]
  ">
  <xsl:value-of select="concat('&#xA;',.,': ')"/>

  <xsl:for-each select="key('kAtrByVal',.)">
   <xsl:value-of select="../@name"/>
   <xsl:if test="not(position()=last())">
    <xsl:text>, </xsl:text>
   </xsl:if>
  </xsl:for-each>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>

当应用于提供的 XML 文档时

<items>
    <item>
        <name>item1</name>
        <attributes>a,b,c,d</attributes>
    </item>
    <item>
        <name>item2</name>
        <attributes>c,d,e</attributes>
    </item>
</items>

产生所需的、正确的结果:

a: item1
b: item1
c: item1, item2
d: item1, item2
e: item2

解释:

  1. 第一遍扫描:标记化和最终结果:

<groups>
  <group name="item1">
    <attr>a</attr>
    <attr>b</attr>
    <attr>c</attr>
    <attr>d</attr>
  </group>
  <group name="item2">
    <attr>c</attr>
    <attr>d</attr>
    <attr>e</attr>
  </group>
</groups>

2. Pass2 接受 Pass1 的结果(使用扩展函数 ext:node-set() 转换为节点集)作为输入,执行 Muenchian 分组并生成最终所需的结果。


仍然在这方面有一些困难。 <xsl:template match =“text()/> 是用于在调用 <xsl:apply-templates select =“$vPass1 / *”/> 时,Muenchian分组模板可以完成其工作吗? - ratherOCD
@ratherOCD:不,这个模板只是覆盖了XSLT内置的文本节点模板 - 并确保文本节点不会作为<xsl:apply-templates/>的结果输出。 - Dimitre Novatchev
谢谢,我已经把所有东西都搞定了。有没有一种好的方法可以按字母顺序对属性进行排序? - ratherOCD
@ratherOCD:是的,请将 xsl:apply-templates select="$vPass1/*/> 替换为 <xsl:apply-templates select="$vPass1/*/*/attr"><xsl:sort/></xsl:apply-templates> - Dimitre Novatchev

0

我的第一个想法是进行两次遍历。首先,使用(稍微)修改过的{{link1:@Alejandro在之前问题的回答}}对attributes元素进行标记化:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <items>
            <xsl:apply-templates/>
        </items>
    </xsl:template>
    <xsl:template match="item">
        <item name="{name}">
            <xsl:apply-templates select="attributes"/>
        </item>
    </xsl:template>
    <xsl:template match="attributes" name="tokenize">
        <xsl:param name="text" select="."/>
        <xsl:param name="separator" select="','"/>
        <xsl:choose>
            <xsl:when test="not(contains($text, $separator))">
                <val>
                    <xsl:value-of select="normalize-space($text)"/>
                </val>
            </xsl:when>
            <xsl:otherwise>
                <val>
                    <xsl:value-of select="normalize-space(
                        substring-before($text, $separator))"/>
                </val>
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="text" select="substring-after(
                                    $text, $separator)"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

这将产生:

<items>
    <item name="item1">
        <val>a</val>
        <val>b</val>
        <val>c</val>
        <val>d</val>
    </item>
    <item name="item2">
        <val>c</val>
        <val>d</val>
        <val>e</val>
    </item>
</items>

然后将以下样式表应用于该输出:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>
    <xsl:key name="byVal" match="val" use="." />
    <xsl:template match="val[generate-id() = 
                             generate-id(key('byVal', .)[1])]">
        <xsl:value-of select="." />
        <xsl:text> : </xsl:text>
        <xsl:apply-templates select="key('byVal', .)" mode="group" />
        <xsl:text>&#10;</xsl:text>
    </xsl:template>
    <xsl:template match="val" mode="group">
        <xsl:value-of select="../@name" />
        <xsl:if test="position() != last()">
            <xsl:text>, </xsl:text>
        </xsl:if> 
    </xsl:template>
    <xsl:template match="val" />
</xsl:stylesheet>

生成:

a : item1
b : item1
c : item1, item2
d : item1, item2
e : item2

在一个样式表中完成这个任务需要更多的思考(或者使用扩展函数)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接