Select unique values ​​from multiple attributes in an XML document using XSLT 1.0

I have the following XML:

<League>
  <Week Date="26/04/2010 19:00">
    <Fixture Id="542" HomeTeamId="371" HomeTeam="London Raiders Green" AwayTeamId="13" AwayTeam="Richmond Swingers" />
    <Fixture Id="543" HomeTeamId="45" HomeTeam="Spartans" AwayTeamId="15" AwayTeam="Panthers" />
    <Fixture Id="544" HomeTeamId="370" HomeTeam="Fat Cats" AwayTeamId="381" AwayTeam="London Raiders Orange" />
  </Week>
  <Week Date="27/04/2010 19:00">
    <Fixture Id="548" HomeTeamId="3" HomeTeam="The Mob" AwayTeamId="81" AwayTeam="London Raiders Red" />
    <Fixture Id="549" HomeTeamId="373" HomeTeam="Intellect" AwayTeamId="83" AwayTeam="Tornadoes" />
  </Week>
</League>

What I need to do is get a unique list of all the command identifiers in this XML document, but the problem is that the command identifiers can be displayed in the attributes HomeTeamIdor AwayTeamIdin the nodes of the device. Therefore, I try to use standard grouping methods ( Grouping using the Muenchian method or selecting unique nodes by checking the previous brother ).

I can get a list of all identifiers in this way:

<xsl:for-each select="//Fixture/@HomeTeamId | //Fixture/@AwayTeamId">
    <xsl:sort select="."/>
    <xsl:value-of select="."/><br/>
</xsl:for-each>

But, of course, when teams appear in more than one fixture, their identifier is displayed more than once, using the above for each of them.

- , , XSLT - ... - ?

+3
2

Muenchian:

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

<xsl:output indent="yes"/>

<xsl:key name="k1" match="Fixture/@HomeTeamId | Fixture/@AwayTeamId" use="."/>

<xsl:template match="/">
  <html>
    <head>
      <body>
        <xsl:for-each select="(//Fixture/@HomeTeamId | //Fixture/@AwayTeamId)[generate-id() = generate-id(key('k1', .)[1])]">
          <xsl:sort select="." data-type="number"/>
          <xsl:value-of select="."/>
          <br/>
        </xsl:for-each>
      </body>
    </head>
  </html>
</xsl:template>

</xsl:stylesheet>

for-each, , , , , , Muenchian grouping .

+3

, :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my" exclude-result-prefixes="my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kTeamById"
  match="@*[name()=document('')/*/my:teamIdNames/*]" use="."/>

 <my:teamIdNames>
   <name>HomeTeamId</name>
   <name>AwayTeamId</name>
 </my:teamIdNames>

 <xsl:variable name="vAttrNames" select=
  "document('')/*/my:teamIdNames/*"/>

 <xsl:template match="/">
  <xsl:apply-templates select=
   "//Fixture/@*[name()=$vAttrNames]"/>
 </xsl:template>

 <xsl:template match=
 "@*[name()=document('')/*/my:teamIdNames/*]
     [generate-id()
     =
      generate-id(key('kTeamById', .)[1])
     ]
 ">
  <xsl:value-of select="."/><br />
 </xsl:template>
</xsl:stylesheet>

, XML-:

<League>
    <Week Date="26/04/2010 19:00">
        <Fixture Id="542" HomeTeamId="371"
        HomeTeam="London Raiders Green"
        AwayTeamId="13" AwayTeam="Richmond Swingers"/>
        <Fixture Id="543" HomeTeamId="45"
        HomeTeam="Spartans" AwayTeamId="15"
        AwayTeam="Panthers"/>
        <Fixture Id="544" HomeTeamId="370"
        HomeTeam="Fat Cats" AwayTeamId="381"
        AwayTeam="London Raiders Orange" />
    </Week>
    <Week Date="27/04/2010 19:00">
        <Fixture Id="548" HomeTeamId="3"
        HomeTeam="The Mob" AwayTeamId="81"
        AwayTeam="London Raiders Red"/>
        <Fixture Id="549" HomeTeamId="373"
        HomeTeam="Intellect" AwayTeamId="83"
        AwayTeam="Tornadoes"/>
    </Week>
</League>

:

371<br/>13<br/>45<br/>15<br/>370<br/>381<br/>3<br/>81<br/>373<br/>83<br/>

:

, HomeTeamId AwayTeamId.

, ( ), , .

+2

Source: https://habr.com/ru/post/1790207/


All Articles