How to check all permutations of space-separated values ​​in XSLT?

I need to enable all permutations when testing possible values ​​(separated by a space) for variable in xsl:when.

For instance:

<xsl:when test="$var='A B C' 
             or $var='B A C' 
             or $var='...' 
             or ...>
    <xsl:value-of select="X+Z"/>

Is there a smart and easy way to do this?

+4
source share
6 answers

Instead of trying to generate all permutations, I would check if all source values ​​will be present in the target and that both sources and targets contain the same number of values.

This is a bit verbose in XSLT 1.0, but still:

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

<xsl:param name="delimiter" select="' '"/>

<xsl:variable name="source" select="'A B C'"/>
<xsl:variable name="target" select="'B A C'"/>

<xsl:variable name="every-source-in-target">
    <xsl:call-template name="every-source-in-target">
        <xsl:with-param name="source" select="$source"/>
        <xsl:with-param name="target" select="$target"/>
    </xsl:call-template>        
</xsl:variable>

<xsl:variable name="count-source" select="string-length(translate($source, translate($source, $delimiter, ''), ''))" />
<xsl:variable name="count-target" select="string-length(translate($target, translate($target, $delimiter, ''), ''))" /> 

<xsl:template match="/">
    <result>
        <xsl:if test="$every-source-in-target='true' and $count-source=$count-target ">MATCH</xsl:if>
    </result>
</xsl:template>

<xsl:template name="every-source-in-target">
    <xsl:param name="source"/>
    <xsl:param name="target"/>
    <xsl:param name="delimiter" select="' '"/>
    <xsl:variable name="token" select="substring-before(concat($source, $delimiter), $delimiter)" />
    <xsl:choose>
        <xsl:when test="not(contains(concat($delimiter, $target, $delimiter), concat($delimiter, $token, $delimiter)))">
            <xsl:value-of select="false()"/>
        </xsl:when>
        <xsl:when test="contains($source, $delimiter)">
            <!-- recursive call -->
            <xsl:call-template name="every-source-in-target">
                <xsl:with-param name="source" select="substring-after($source, $delimiter)"/>
                <xsl:with-param name="target" select="$target"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="true()"/>
        </xsl:otherwise>
   </xsl:choose>
</xsl:template>

</xsl:stylesheet>

, : , "A B B C" "B A A C" . , , IMHO, :

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:param name="delimiter" select="' '"/>

<xsl:variable name="source" select="'A B C'"/>
<xsl:variable name="target" select="'B A C'"/>

<xsl:variable name="sorted-source">
    <xsl:call-template name="sort-list">
        <xsl:with-param name="list" select="$source"/>
    </xsl:call-template>        
</xsl:variable>

<xsl:variable name="sorted-target">
    <xsl:call-template name="sort-list">
        <xsl:with-param name="list" select="$target"/>
    </xsl:call-template>        
</xsl:variable>

<xsl:template match="/">
    <result>
        <xsl:if test="$sorted-source=$sorted-target">MATCH</xsl:if>
    </result>
</xsl:template>

<xsl:template name="sort-list">
    <xsl:param name="list"/>
    <!-- tokenize the list -->
    <xsl:variable name="tokens">
        <xsl:call-template name="tokenize">
            <xsl:with-param name="text" select="$list"/>
        </xsl:call-template>        
    </xsl:variable>
    <!-- re-assemble the list in alphabetic order -->
    <xsl:for-each select="exsl:node-set($tokens)/token">
        <xsl:sort select="." data-type="text" order="ascending"/>
        <xsl:value-of select="."/>
        <xsl:if test="position()!=last">
            <xsl:value-of select="$delimiter"/>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

<xsl:template name="tokenize">
    <xsl:param name="text"/>
    <xsl:param name="delimiter" select="' '"/>
        <xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
        <xsl:if test="$token">
            <token>
                <xsl:value-of select="$token"/>
            </token>
        </xsl:if>
        <xsl:if test="contains($text, $delimiter)">
            <!-- recursive call -->
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
            </xsl:call-template>
        </xsl:if>
</xsl:template>

</xsl:stylesheet>
+1

, ​​ : fooobar.com/questions/1629069/...

, XML- .

:

  • .
  • .
  • XML- - , .

" ". /*/* ( XML-) - .

, , .

XML-, , prtfData​​p >

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

 <xsl:param name="prtfData">
   <v>A</v>
   <v>B</v>
   <v>C</v>
 </xsl:param>

 <xsl:variable name="vData" select="document('')/*/xsl:param[@name = 'prtfData']/*"/>
 <xsl:variable name="vnumItems" select="count($vData)"/>
 <xsl:variable name="vTotalLength" select="string-length($prtfData) + $vnumItems -1"/>

 <xsl:variable name="vnumPermutations">
   <xsl:call-template name="factorial">
     <xsl:with-param name="pN" select="$vnumItems"/>
   </xsl:call-template>
 </xsl:variable>

  <xsl:template match="/*">
    <xsl:if test="not(count(*) = $vnumPermutations)">
      <xsl:message terminate="yes">
         Error: The count of /*/* is not <xsl:value-of select="$vnumPermutations"/>
      </xsl:message>
    </xsl:if>

    <xsl:if test="v[not(string-length() = $vTotalLength)]">
      <xsl:message terminate="yes">
         The input item "<xsl:value-of select="v[not(string-length() = $vTotalLength)]"/>" <xsl:text/>
         <xsl:text/>has string-length not-equal to <xsl:text/>
         <xsl:value-of select="$vTotalLength"/>
      </xsl:message>
    </xsl:if>

    <xsl:variable name="vInput" select="/*/*"/>

    <xsl:for-each select="$vData">
      <xsl:variable name="vPaddedItem" select="concat(' ', ., ' ')"/>
      <xsl:if test="$vInput[not(contains(concat(' ', ., ' '), $vPaddedItem))]">
          <xsl:message terminate="yes">
             Error: The data item "<xsl:value-of select="."/>" isn't contained in <xsl:text/>
             <xsl:value-of select="$vInput[not(contains(concat(' ', ., ' '), $vPaddedItem))]"/>.
          </xsl:message>
      </xsl:if>
    </xsl:for-each>

    <xsl:if test="$vInput[. = preceding-sibling::* or . = following-sibling::*]">
          <xsl:message terminate="yes">
             Error: Some data items are equal. Not all permutations represented.
          </xsl:message>
    </xsl:if>

    Valid input.
  </xsl:template>

  <xsl:template name="factorial">
    <xsl:param name="pN" select="1"/>
    <xsl:param name="pResult" select="1"/>

    <xsl:choose>
        <xsl:when test="not($pN > 0)">
          <xsl:value-of select="$pResult"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="factorial">
            <xsl:with-param name="pN" select="$pN -1"/>
            <xsl:with-param name="pResult" select="$pN * $pResult"/>
          </xsl:call-template>
        </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

XML:

<t>
  <v>C B A</v>
  <v>C A B</v>
  <v>B A C</v>
  <v>B C A</v>
  <v>A C B</v>
  <v>A B C</v>
</t>

:

.

XML-:

<t>
  <v>C B A</v>
  <v> C A B </v>
  <v>B A C</v>
  <v>B C A</v>
  <v>A C B</v>
  <v>A B C</v>
</t>

:

"C A B" , 5

XML-:

<t>
  <v>C B A</v>
  <v>C C B</v>
  <v>B A C</v>
  <v>B C A</v>
  <v>A C B</v>
  <v>A B C</v>
</t>

:

: "A" C C B.

XML-:

<t>
  <v>C B A</v>
  <v>C C B</v>
  <v>B A C</v>
</t>

:

: /*/* 6

, XML-:

<t>
  <v>C B A</v>
  <v>C A B</v>
  <v>C A B</v>
  <v>B A C</v>
  <v>A C B</v>
  <v>A B C</v>
</t>

:

: . .

+1

, 2.0 deep-equals():

deep-equal(f:to-atts(source), f:to-atts(target))

<xsl:function f:to-atts as="xs:boolean">
  <xsl:param name="in" as="xs:string">
  <e>
   <xsl:for-each select="tokenize($in, ' ')">
     <xsl:attribute name="." select="0"/>
   </xsl:for-each>
  </e>
</xsl:function>

. : "A A" "A". , .

+1

- , :

<xsl:variable name="tempVar" select="concat(' ',$var,' ')"/>

<xsl:when test="contains($tempVar,' A ') and
                contains($tempVar, ' B ') and
                contains($tempVar, ' C ')">
  <xsl:value-of select="X+Z"/>
</xsl:when>

tempVar , , / , .

, , concat test.

+1

XSLT 2.0, : O(N) vs. O(N*log(N))

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:my="my:my">
 <xsl:output method="text"/>
 <xsl:key name="kAllItems" match="x" use="."/>

  <xsl:param name="pData">
   <x>A</x>
   <x>B</x>
   <x>C</x>
 </xsl:param>

  <xsl:template match="v">
    (<xsl:value-of select="."/>) ==> <xsl:sequence 
                                          select="my:exactPremutation(., $pData)"/>
  </xsl:template>

  <xsl:function name="my:exactPremutation" as="xs:boolean">
    <xsl:param name="pInput" as="xs:string"/>
    <xsl:param name="pDataItems" as="document-node()"/>

    <xsl:variable name="vNumDataItems" select="count($pDataItems/*)"/>

    <xsl:variable name="vMatches" as="xs:integer*">
      <xsl:for-each-group select="tokenize($pInput, '\s+')" group-by=".">
        <xsl:variable name="vMatchedDataItem" 
                      select="key('kAllItems', current-grouping-key(), $pDataItems)"/>

          <xsl:sequence select="1[not(current-group()[2]) and $vMatchedDataItem]"/>
          <xsl:sequence select="($vNumDataItems +1)[not($vMatchedDataItem)]"/>
      </xsl:for-each-group>
    </xsl:variable>

    <xsl:sequence select="sum($vMatches) eq $vNumDataItems"/>
  </xsl:function>
</xsl:stylesheet>

XML-:

<t>
  <!-- Correct -->
   <v>A B C</v>
   <v>A C B</v>
   <v>B A C</v>
   <v>B C A</v>
   <v>C A B</v>
   <v>C B A</v>
  <!-- Incorrect -->
   <v>C A C</v>
   <v>A B</v>
   <v></v>
   <v>A B C D</v>   
</t>

, :

(A B C) ==> true

(A C B) ==> true

(B A C) ==> true

(B C A) ==> true

(C A B) ==> true

(C B A) ==> true


(C A C) ==> false

(A B) ==> false

() ==> false

(A B C D) ==> false

Note . Evaluation of linear efficiency suggests that the XSLT processor uses an efficient (for example, based on a hash table) grouping implementation.

+1
source

Here's a simple XPath 1.0 solution:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:variable name="x" select="'B A C'"/>
  <xsl:variable name="y" select="'A B C'"/>

  <xsl:template match="/">
    <xsl:choose>
      <xsl:when test="string-length($x) = string-length($y)
                      and translate($x, $y, '') = ''">
        <xsl:message>Same</xsl:message>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message>Different</xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

Note that this assumes that variables with a unique char value are rearranged.

0
source

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


All Articles