简体   繁体   中英

how to split a string based on a delimiter using XQuery

I have this xml returned from sql :

<Keywords>
  <Record CIMtrek_Keywords="Bus Dev|Ser Del|Sol Del|?" />
</Keywords>

and i want to put these values one by one in combo box, sample code is below which has got Increase and Decrease as value same way i would like to have Bus Dev and Ser Del and Sol Del and ? one by one

<select size="1" style="width:60%;" name="CIMtrek_CI_CER_Type"
                                                id="CIMtrek_CI_CER_Type">
                                                <option value="0">Select Fund Type</option>
                                                <option value="Increase">
                                                    <xsl:if test="//Record/CIMtrek_CI_CER_Type/text()='Increase'">
                                                        <xsl:attribute name="selected">true</xsl:attribute>
                                                    </xsl:if>
                                                    Increase
                                                </option>
                                                <option value="Decrease">
                                                    <xsl:if
                                                        test="//Record/CIMtrek_CI_CER_Type/text()='Decrease'">
                                                        <xsl:attribute name="selected">true</xsl:attribute>
                                                    </xsl:if>
                                                    Decrease
                                                </option>
                                            </select>

This is how i created combo box from the values which is in xml generated from SQL query :

<xsl:for-each select="//CIMtrek_CarrierName/Record">
                                                    <option>
                                                        <xsl:if
                                                            test="//Record/CIMtrek_RegWhseCo_Carrier/text()=@CIMtrek_CarrierName">
                                                            <xsl:attribute name="selected">true</xsl:attribute>
                                                        </xsl:if>
                                                        <xsl:attribute name="value"><xsl:value-of
                                                            select="@CIMtrek_CarrierName" />!@#$%^*!<xsl:value-of
                                                            select="@Carrier_LateContacts" />!@#$%^*!<xsl:value-of
                                                            select="@Carrier_Contacts1" />!@#$%^*!<xsl:value-of
                                                            select="@Carrier_Contacts2" />!@#$%^*!<xsl:value-of
                                                            select="@Carrier_Contacts3" /></xsl:attribute>
                                                        <xsl:value-of select="@CIMtrek_CarrierName" />
                                                    </option>
                                                </xsl:for-each>

with BeniBela answer i am not able to put things in, like the above code,

Hope BeniBela can help me to figure it out.

How to do this,

Please help me,

Best Regards.

The tokenize() function does exactly this. Note that takes a regular expression, so | has to be escaped as [|] leading to:

for $o in /Keywords/Record/tokenize(@CIMtrek_Keywords, "[|]") return
  <option value="{$o}">{$o}</option>

That new xml can be converted to xquery like this:

for $r in //CIMtrek_CarrierName/Record return  $r/
  <option value="{@CIMtrek_CarrierName}!@#$%^*!{@Carrier_LateContacts}!@#$%^*!{@Carrier_Contacts1}!@#$%^*!{@Carrier_Contacts2}!@#$%^*!{@Carrier_Contacts3}">
  {  if (//Record/CIMtrek_RegWhseCo_Carrier/text()=@CIMtrek_CarrierName) then attribute selected { "true" } else (),
     data(@CIMtrek_CarrierName)  }
  </option>

The intitial $r/ changes the context, so we do not need to write $r/ before every attribute access, the xsl:value-of becomes {..} and xsl:attribute becomes an xml-like attribute declaration or 'attribute name {..}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM