【问题标题】:XSLT 1.0 to iterate over several XML elements with comma delimited valuesXSLT 1.0 使用逗号分隔值遍历多个 XML 元素
【发布时间】:2011-04-06 01:53:37
【问题描述】:

我有一个如下结构的 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() 函数,只是意识到它也不允许我遍历单个 Attribute 节点。

在这一点上,我找不到一种简单的方法来做到这一点,并且非常感谢任何关于如何继续的帮助或想法。谢谢!

【问题讨论】:

  • 好问题,+1。请参阅我的答案以获得完整的解决方案和解释。
  • 我喜欢思考这个问题

标签: xslt xpath


【解决方案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. Pass1:标记化和最终结果:

<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 分组并产生最终的所需结果。

【讨论】:

  • 这个还是有点麻烦。是否使用了&lt;xsl:template match="text()"/&gt;,以便在调用&lt;xsl:apply-templates select="$vPass1/*"/&gt; 时,Muenchian 分组模板可以做它的事情?
  • @ratherOCD:不,这个模板只是覆盖了文本节点的 XSLT 内置模板——并确保文本节点不会作为&lt;xsl:apply-templates/&gt;的结果输出
  • 谢谢,一切正常。有没有按字母顺序对 attr 进行排序的好方法?
  • @ratherOCD:是的,将xsl:apply-templates select="$vPass1/*/&gt; 替换为&lt;xsl:apply-templates select="$vPass1/*/*/attr"&gt; &lt;xsl:sort/&gt; &lt;/xsl:apply-templates&gt;
【解决方案2】:

我的第一个想法是通过两次。首先,使用@Alejandro's answer to this previous question 的(略微)修改版本标记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

在一个样式表中这样做需要更多的思考(或扩展功能)。

【讨论】:

    猜你喜欢
    • 2014-03-11
    • 1970-01-01
    • 2021-12-30
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多