2016-09-29 6 views
0

ich folgende XSLT haben, die die Ausgabeformate von Tasten auf einem bestimmten Array Gruppierung:Flucht Eingang in XSLT

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:mf="http://example.com/mf" 
    exclude-result-prefixes="xs mf"> 


    <xsl:param name="values" select="'Sniper 50 Red', 'Xiper 10 Red'"/> 

    <xsl:output indent="yes"/> 

    <xsl:function name="mf:match" as="xs:string"> 
     <xsl:param name="input" as="xs:string"/> 
     <xsl:param name="values" as="xs:string*"/> 
     <xsl:sequence 
      select="if ($values[matches($input, concat('^', .))]) 
        then $values[matches($input, concat('^', .))] 
        else $input"/> 
    </xsl:function> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="results"> 
     <xsl:copy> 
      <xsl:for-each-group select="pageFunctionResult" group-by="mf:match(product_name, $values)"> 
       <xsl:choose> 
        <xsl:when test="product_name ne current-grouping-key()"> 
          <xsl:copy> 
           <position>MAINPRODUCT</position> 
           <variantname> 
            <xsl:value-of select="product_name"/> 
           </variantname> 
           <product_namea> 
            <xsl:value-of select="current-grouping-key()"/> 
           </product_namea> 
           <xsl:copy-of select="category, product_id, quantity, manufacturer,image"/> 
           <xsl:apply-templates select="current-group() except ."/> 
          </xsl:copy> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:for-each select="current-group()"> 
         <xsl:copy> 
          <position>MAINPRODUCT</position> 
          <variantname> 
           <xsl:value-of select="current-grouping-key()"/> 
          </variantname> 
          <xsl:apply-templates select="*"/> 
         </xsl:copy> 
         </xsl:for-each> 
        </xsl:otherwise> 
       </xsl:choose> 
      </xsl:for-each-group> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="pageFunctionResult"> 
     <variant id="{position()}"> 
      <xsl:apply-templates/> 
     </variant> 
    </xsl:template> 

</xsl:transform> 

Hier können Sie sehen it in action. Es funktioniert gut, aber ich jetzt Schlüssel mit Halterung in ihren Namen haben:

<xsl:param name="values" select="'Sniper 50 Red (26)', 'Xiper 10 Red (27)'"/> 

... und dies offensichtlich bremst den Code, weil Klammern sind in regex vorbehalten. Also sollten die Klammern maskiert werden. Ich habe einen Tipp zu verwenden functx:escape-for-regex, aber ich bin nicht in der Lage, es in den Code zu implementieren.

Antwort

1

Dieses Beispiel zeigt, wie die functx-Funktion:

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:mf="http://example.com/mf" 
    xmlns:functx="http://www.functx.com" 
    exclude-result-prefixes="xs mf functx"> 

    <xsl:param name="values" select="'Sniper 50 Red (26)', 'Xiper 10 Red (27)'"/> 

    <xsl:output indent="yes"/> 

    <xsl:function name="functx:escape-for-regex" as="xs:string" 
     > 
     <xsl:param name="arg" as="xs:string?"/> 

     <xsl:sequence select=" 
      replace($arg, 
      '(\.|\[|\]|\\|\||\-|\^|\$|\?|\*|\+|\{|\}|\(|\))','\\$1') 
      "/> 

    </xsl:function> 

    <xsl:function name="mf:match" as="xs:string"> 
     <xsl:param name="input" as="xs:string"/> 
     <xsl:param name="values" as="xs:string*"/> 
     <xsl:sequence 
      select="if ($values[matches($input, concat('^', functx:escape-for-regex(.)))]) 
      then $values[matches($input, concat('^', functx:escape-for-regex(.)))] 
      else $input"/> 
    </xsl:function> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="results"> 
     <xsl:copy> 
      <xsl:for-each-group select="pageFunctionResult" group-by="mf:match(product_name, $values)"> 
       <xsl:choose> 
        <xsl:when test="product_name ne current-grouping-key()"> 
         <xsl:copy> 
          <position>MAINPRODUCT</position> 
          <variantname> 
           <xsl:value-of select="product_name"/> 
          </variantname> 
          <product_namea> 
           <xsl:value-of select="current-grouping-key()"/> 
          </product_namea> 
          <xsl:copy-of select="category, product_id, quantity, manufacturer,image"/> 
          <xsl:apply-templates select="current-group() except ."/> 
         </xsl:copy> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:for-each select="current-group()"> 
          <xsl:copy> 
           <position>MAINPRODUCT</position> 
           <variantname> 
            <xsl:value-of select="current-grouping-key()"/> 
           </variantname> 
           <xsl:apply-templates select="*"/> 
          </xsl:copy> 
         </xsl:for-each> 
        </xsl:otherwise> 
       </xsl:choose> 
      </xsl:for-each-group> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="pageFunctionResult"> 
     <variant id="{position()}"> 
      <xsl:apply-templates/> 
     </variant> 
    </xsl:template> 

</xsl:transform> 
+1

Die Funktion von http://www.xsltfunctions.com/xsl/functx_escape-for-regex.html genommen, ich habe nicht versucht, zu ändere es, als der Hauptvorschlag war, die Bibliotheksfunktion zu verwenden und dann zu zeigen, wie es gemacht wird, da das Plakat sagte, dass er nicht wusste, wie. Ich stimme zu, dass Ihre Versionen kompakter und effizienter aussehen. –

+0

Ich verstehe. Okay, es ist ein kleiner Punkt, ich entferne den Kommentar. – Tomalak