开发者

Get unique values using XSLT 1.0 (Without using XSL:Key)

开发者 https://www.devze.com 2023-04-10 12:48 出处:网络
I\'m facing a typical problem while am getting the Unique list using XSLT 1.0. Sample XSLT: 开发者_Go百科<xsl:if test=\"$tempVar = \'true\'\">

I'm facing a typical problem while am getting the Unique list using XSLT 1.0.

Sample XSLT:

开发者_Go百科<xsl:if test="$tempVar = 'true'">
    <xsl:variable name="filePath" select="document($mPath)" />
    // Do something
    // I can't implement this using "Muenchian Method". 
    // Since, I can't declare <xsl:key> inside of <xsl:if>
    // There is no chance to declare <xsl:key> on top.
    // I should get unique list from here only
</xsl:if>

filepath variable would contain XML as follows:-

<Root>
    <Data id="102">
        <SubData>
            <Info code="abc">Information 102</Info>
        </SubData>
    </Data>
    <Data id="78">
        <SubData>
            <Info code="def">Information 78</Info>
        </SubData>
    </Data>
    <Data id="34">
        <SubData>
            <Info code="abc">Information 34</Info>
        </SubData>
    </Data>
    <Data id="55">
        <SubData>
            <Info code="xyz">Information 55</Info>
        </SubData>
    </Data>
    <Data id="86">
        <SubData>
            <Info code="def">Information 86</Info>
        </SubData>
    </Data>
    <Data id="100">
        <SubData>
            <Info code="xyz">Information 100</Info>
        </SubData>
    </Data>
</Root>

Output: Unique list of code should be

abc
def
xyz

Thanks


<xsl:if test="$tempVar = 'true'">
    <xsl:variable name="filePath" select="document($mPath)" />
    // Do something
    // I can't implement this using "Muenchian Method". 
    // Since, I can't declare <xsl:key> inside of <xsl:if>
    // There is no chance to declare <xsl:key> on top.
    // I should get unique list from here only
</xsl:if>

It isn't true that one cannot use <xsl:key> and the key() function in such circumstances:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kcodeByVal" match="@code" use="."/>

 <xsl:variable name="tempVar" select="'true'"/>

 <xsl:variable name="vrtfFilePath">
    <Root>
        <Data id="102">
            <SubData>
                <Info code="abc">Information 102</Info>
            </SubData>
        </Data>
        <Data id="78">
            <SubData>
                <Info code="def">Information 78</Info>
            </SubData>
        </Data>
        <Data id="34">
            <SubData>
                <Info code="abc">Information 34</Info>
            </SubData>
        </Data>
        <Data id="55">
            <SubData>
                <Info code="xyz">Information 55</Info>
            </SubData>
        </Data>
        <Data id="86">
            <SubData>
                <Info code="def">Information 86</Info>
            </SubData>
        </Data>
        <Data id="100">
            <SubData>
                <Info code="xyz">Information 100</Info>
            </SubData>
        </Data>
    </Root>
 </xsl:variable>

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

 <xsl:template match="/">
  <xsl:if test="$tempVar = 'true'">
     <xsl:for-each select="$vfilePath">
      <xsl:for-each select=
       "*/*/*/Info/@code
                    [generate-id()
                    =
                     generate-id(key('kcodeByVal',.)[1])
                     ]
       ">
       <xsl:value-of select="concat(.,' ')"/>
      </xsl:for-each>
     </xsl:for-each>
    </xsl:if>
 </xsl:template>

</xsl:stylesheet>

when this transformation is applied to any XML document (not used in this example), the wanted, correct result is produced:

abc def xyz 


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

    <xsl:template match="/">
        <xsl:apply-templates select="//Data[
                             not(
                                */Info/@code = preceding-sibling::Data/*/Info/@code
                             )
                         ]/*/Info/@code"/>
    </xsl:template>

    <xsl:template match="@*">
        <xsl:value-of select="."/>
        <xsl:text>&#xD;</xsl:text>
    </xsl:template>

</xsl:stylesheet>


Your reason for not using the Muenchian method or xsl:key is spurious. It will work perfectly well. You have probably failed to understand that when you declare a key definition, it is not specific to one particular source document, it allows you to use the key() function against any source document.

0

精彩评论

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