开发者

XSLT 1.0 to iterate over several XML elements with comma delimited values

开发者 https://www.devze.com 2023-02-22 03:09 出处:网络
I have an XML document structured as follows <items> <item> <name>item1</name> <attributes>a,b,c,d</attributes>

I have an XML document structured as follows

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

For each unique attribute 开发者_开发问答value (delimited by commas) I need to list all item names associated with that value like so:

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

My initial plan was to use a template to parse the attributes into Attribute nodes, surrounding each with appropriate tags, and then separating out the unique values with an XPATH expression like

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

but since the result of the template isn't a node-set that ever goes through an XML parser, I can't traverse it. I also tried exslt's node-set() function only to realize it does not allow me to traverse the individual Attribute nodes either.

At this point I'm at a loss for a simple way to do this and would really appreciate any help or ideas on how to proceed. Thanks!


This transformation:

<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>

when applied on the provided XML document:

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

produces the wanted, correct result:

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

Explanation:

  1. Pass1: tokenization and end result:

<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 takes the result of Pass1 (converted to a nodeset using the extension function ext:node-set()) as input, performs Muenchian grouping and produces the final, wanted result.


My first thought is to make two passes. First, tokenize the attributes elements using a (slightly) modified version of @Alejandro's answer to this previous question:

<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>

Which produces:

<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>

Then apply the following stylesheet to that output:

<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>

Producing:

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

Doing this in one stylesheet would require more thought (or an extension function).

0

精彩评论

暂无评论...
验证码 换一张
取 消