2011-09-15 17 views
10

में विभाजित फ़ंक्शन XSLT 1.0 में नोड मान को कैसे विभाजित करें?xslt 1.0

<mark>1,2</mark> 

मुझे विभाजन के आउटपुट के प्रत्येक मूल्य के साथ लूप के लिए कुछ संचालन करने की आवश्यकता है।

<xsl:for-each select=""> </xsl:for-each>

यह कैसे करना है?

+0

क्या मूल्य में हमेशा दो आइटम हैं, या यह एक चर संख्या है? –

+0

यह भिन्न हो सकता है। यह नोड चिह्न का मूल्य है। –

+0

अच्छा सवाल, +1। XSLT (1.0 या 2.0) का कौन सा संस्करण उपयोग किया जाता है, इस पर निर्भर करता है, यह रिकर्सिव प्रोसेसिंग के साथ या मानक XPath 2.0 फ़ंक्शन 'टोकननाइज़() 'का उपयोग कर किया जा सकता है। –

उत्तर

12

मैं XSLT 1.0 समाधान:

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

<xsl:template match="mark"> 
    <xsl:variable name="vrtfSplit"> 
    <xsl:apply-templates/> 
    </xsl:variable> 

    <xsl:for-each select="ext:node-set($vrtfSplit)/*"> 
    <processedItem> 
    <xsl:value-of select="10 * ."/> 
    </processedItem> 
    </xsl:for-each> 
</xsl:template> 

<xsl:template match="text()" name="split"> 
    <xsl:param name="pText" select="."/> 
    <xsl:if test="string-length($pText) >0"> 
    <item> 
    <xsl:value-of select= 
     "substring-before(concat($pText, ','), ',')"/> 
    </item> 

    <xsl:call-template name="split"> 
    <xsl:with-param name="pText" select= 
    "substring-after($pText, ',')"/> 
    </xsl:call-template> 
    </xsl:if> 
</xsl:template> 
</xsl:stylesheet> 
:

यहाँ एक तरह से केवल xxx:node-set() विस्तार समारोह का उपयोग कर XSLT 1.0 में यह करने के लिए है

जब यह परिवर्तन व्यावहारिक निम्नलिखित XML दस्तावेज़ लिए आवेदन किया है:

<mark>1,2,3,4,5</mark> 

चाहता था, सही निर्गम (प्रत्येक आइटम 10 से गुणा) उत्पादन किया जाता है:

द्वितीय। XSLT 2.0 समाधान:

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

<xsl:template match="mark"> 
    <xsl:for-each select="tokenize(., ',')"> 
    <processedItem> 
    <xsl:sequence select="10*xs:integer(.)"/> 
    </processedItem> 
    </xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 
1

यदि आप exslt का उपयोग कर सकते हैं तो tokenize() फ़ंक्शन है जो यह अच्छी तरह से करेगा।

node-set str:tokenize(string, string?) 

देखें http://www.exslt.org/str/functions/tokenize/

1

1,0 में आप एक पुनरावर्ती टेम्पलेट लिखने के लिए की जरूरत है - क्योंकि यह पहले से ही लिखा गया है, को छोड़कर आप नहीं। http://www.exslt.org से str:tokenize टेम्पलेट डाउनलोड करें।

4

Dimitre Novatchev द्वारा explaination भयानक है, लेकिन हम भी एक नज़र node-set() समारोह का उपयोग किए बिना और अधिक सरल तरीके में कर सकते हैं है

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


    <xsl:output omit-xml-declaration="yes" indent="yes"/> 



    <xsl:variable name="delimiter"> 
     <xsl:text>,</xsl:text> 
    </xsl:variable> 



    <xsl:template match="mark"> 
     <xsl:variable name="dataList"> 
      <xsl:value-of select="."/> 
     </xsl:variable> 
     <xsl:call-template name="processingTemplate"> 
      <xsl:with-param name="datalist" select="$dataList"/> 
     </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="processingTemplate"> 
     <xsl:param name="datalist"/> 


     <xsl:choose> 
     <xsl:when test="contains($datalist,$delimiter) "> 
       <xsl:element name="processedItem"> 
        <xsl:value-of select="substring-before($datalist,$delimiter) * 10"/> 
       </xsl:element> 
       <xsl:call-template name="processingTemplate"> 
        <xsl:with-param name="datalist" select="substring-after($datalist,$delimiter)"/> 
       </xsl:call-template> 
     </xsl:when> 
      <xsl:when test="string-length($datalist)=1"> 
       <xsl:element name="processedItem"> 
        <xsl:value-of select="$datalist * 10"/> 

        </xsl:element> 
      </xsl:when> 
     </xsl:choose>  

    </xsl:template> 
</xsl:stylesheet> 
0

इस कोड XSLT 1.0 (में एक सीमांकित स्ट्रिंग विभाजित कर देगा यह 2.0 के लिए काम करेगा, लेकिन नोड-सेट का उपयोग न करें।) यह स्ट्रिंग या वैकल्पिक रूप से तत्वों के ऊपरी मामले में खाली तत्वों को वैकल्पिक रूप से दबाएगा।

<!-- Example delimited string. --> 
<xsl:variable name="delimitedString" select="'a, b, c, , , d, e, f, g'"/> 

<!-- Create a node set where each node contains one of the elements from the 
    delimited string. --> 
<xsl:variable name="splitNodes"> 
    <xsl:call-template name="getNodeListFromDelimitedList"> 
    <xsl:with-param name="inStrList" select="$delimitedString"/> 
    <xsl:with-param name="delimiter" select="','"/> 
    <xsl:with-param name="suppressEmptyElements" select="false()"/> 
    <xsl:with-param name="upperCase" select="false()"/> 
    <xsl:with-param name="allTrim" select="false()"/> 
    </xsl:call-template>  
</xsl:variable> 

<!-- Use this for XSLT 1.0 only. --> 
<xsl:variable name="splitNodesList" select="msxml:node-set($splitNodes)"/> 

<!-- Use the split node list to do something. For example, create a string like 
    the delimited string, but without the delimiters. --> 
<xsl:variable name="nonDelimitedString"> 
    <xsl:for-each select="$splitNodesList/element"> 
    <xsl:value-of select="."/> 
    </xsl:for-each> 
</xsl:variable> 


<!-- Do something with the nonDelimitedString. --> 

<!-- 
***************************************************************************************** 

This template converts a delimited string list to a node list as follows: 

Each value in the delimited input string is extracted from the string. Then, a node is 
created to contain the value. The name of the node is 'element', and it is added to the 
list. To use this template, create an variable and call this template from within the variable. 
If you are using XSLT version 1.0, convert the node list to a node set using the node-set 
function. You can access the element as follows: $SomeVariableNodeSet/element 

***************************************************************************************** 
--> 
<xsl:template name="getNodeListFromDelimitedList"> 
    <!-- Delimited string with one or more delimiters. --> 
    <xsl:param name="inStrList"/> 
    <!-- The delimiter. --> 
    <xsl:param name="delimiter" select="'|'"/> 
    <!-- Set to true to suppress empty elements from being added to node list. Otherwise, set to 'false'.--> 
    <xsl:param name="suppressEmptyElements" select="true()"/> 
    <!-- Set to true to upper case the strings added to the node list. --> 
    <xsl:param name="upperCase" select="false()"/> 
    <!-- Set to true to left trim and right trim the strings added to the nodes list. --> 
    <xsl:param name="allTrim" select="false()"/> 

    <xsl:variable name="element"> 
    <xsl:choose> 
     <xsl:when test="contains($inStrList,$delimiter)"> 
     <xsl:value-of select="substring-before($inStrList,$delimiter)"/> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:value-of select="$inStrList"/> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:variable> 

    <!-- Write out the element based on parameters. --> 
    <xsl:if test="not($suppressEmptyElements) or normalize-space($element) != ''"> 
    <!-- Put the element in the list. --> 
    <xsl:element name="element"> 
     <xsl:choose> 
     <xsl:when test="$allTrim"> 
      <xsl:call-template name="all-trim"> 
      <xsl:with-param name="inStr" select="$element"/> 
      <xsl:with-param name="upperCase" select="$upperCase"/> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:when test="$upperCase"> 
      <xsl:value-of select="translate($element, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$element"/> 
     </xsl:otherwise> 
     </xsl:choose> 
    </xsl:element> 
    </xsl:if> 

    <xsl:if test="contains($inStrList,$delimiter)"> 
    <!-- Call template recursively to process the next element. --> 
    <xsl:call-template name="getNodeListFromDelimitedList"> 
     <xsl:with-param name="inStrList" select="substring-after($inStrList,$delimiter)"/> 
     <xsl:with-param name="delimiter" select="$delimiter"/> 
     <xsl:with-param name="suppressEmptyElements" select="$suppressEmptyElements"/> 
     <xsl:with-param name="upperCase" select="$upperCase"/> 
     <xsl:with-param name="allTrim" select="$allTrim"/> 
    </xsl:call-template> 
    </xsl:if> 

</xsl:template> 


<!-- 
***************************************************************************************** 
This template trims the blanks from the left and right sides of a string. 
***************************************************************************************** 
--> 
<xsl:template name="all-trim"> 
    <!-- The string that you want to all trim. --> 
    <xsl:param name="inStr"/> 
    <xsl:param name="upperCase" select="false()"/> 

    <xsl:variable name="leftTrimmed"> 
    <xsl:call-template name="left-trim"> 
     <xsl:with-param name="inStr" select="$inStr"/> 
    </xsl:call-template> 
    </xsl:variable> 

    <xsl:variable name="rightTrimmed"> 
    <xsl:call-template name="right-trim"> 
     <xsl:with-param name="inStr" select="$leftTrimmed"/> 
    </xsl:call-template> 
    </xsl:variable> 

    <xsl:choose> 
    <xsl:when test="$upperCase"> 
     <xsl:value-of select="translate($rightTrimmed, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:value-of select="$rightTrimmed"/> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

<!-- 
***************************************************************************************** 
This template trims the blanks from the left side of a string. 
***************************************************************************************** 
--> 
<xsl:template name="left-trim"> 
    <!-- The string you want to left trim. --> 
    <xsl:param name ="inStr"/> 

    <xsl:choose> 
    <xsl:when test="$inStr!=''"> 
     <xsl:variable name="temp" select="substring($inStr, 1, 1)"/> 
     <xsl:choose> 
     <xsl:when test="$temp=' '"> 
      <xsl:choose> 
      <xsl:when test="string-length($inStr) &gt; 1"> 
       <xsl:call-template name="left-trim"> 
       <xsl:with-param name="inStr" select="substring($inStr, 2, string-length($inStr)-1)"/> 
       </xsl:call-template> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="''"/> 
      </xsl:otherwise> 
      </xsl:choose> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$inStr"/> 
     </xsl:otherwise> 
     </xsl:choose> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:value-of select="''"/> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 


<!-- 
***************************************************************************************** 
This template trims the blanks from the right side of a string. 
***************************************************************************************** 
--> 
<xsl:template name="right-trim"> 
    <!-- The string you want to right trim. --> 
    <xsl:param name ="inStr"/> 

    <xsl:choose> 
    <xsl:when test="$inStr!=''"> 
     <xsl:variable name="temp" select="substring($inStr, string-length($inStr), 1)"/> 
     <xsl:choose> 
     <xsl:when test="$temp=' '"> 
      <xsl:choose> 
      <xsl:when test="string-length($inStr) &gt; 1"> 
       <xsl:call-template name="right-trim"> 
       <xsl:with-param name="inStr" select="substring($inStr, 1, string-length($inStr)-1)"/> 
       </xsl:call-template> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="''"/> 
      </xsl:otherwise> 
      </xsl:choose> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$inStr"/> 
     </xsl:otherwise> 
     </xsl:choose> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:value-of select="''"/> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template>