Encoding XPath Expressions with both single and double quotes

前端 未结 8 1185
孤街浪徒
孤街浪徒 2020-11-30 07:21

XPath (v1) contains no way to encode expressions.

If you only have single OR double quotes then you can use expressions such as

//review[@name=\"Bob         


        
8条回答
  •  既然无缘
    2020-11-30 08:19

    Join in the fun

    public string XPathLiteral(string text) {
    
        const string APOS = "'";
        const string QUOTE = @"""";
    
        int pos = 0;
    
        int posApos;
        int posQuote;
    
        posQuote = text.IndexOf(QUOTE, pos);
    
        if (posQuote < 0) {
            return QUOTE + text + QUOTE;
        }//if
    
        posApos = text.IndexOf(APOS, pos);
    
        if (posApos < 0) {
            return APOS + text + APOS;
        }//if
    
        bool containsApos = posApos < posQuote;
    
        StringBuilder sb = new StringBuilder("concat(", text.Length * 2);
    
        bool loop = true;
        bool comma = false;
    
        while (loop) {
    
            if (posApos < 0) {
                posApos = text.Length;
                loop = false;
            }//if
    
            if (posQuote < 0) {
                posQuote = text.Length;
                loop = false;
            }//if
    
            if (comma) {
                sb.Append(",");
            } else {
                comma = true;
            }//if
    
            if (containsApos) {
                sb.Append(QUOTE);
                sb.Append(text.Substring(pos, posQuote - pos));
                sb.Append(QUOTE);
                pos = posQuote;
                if (loop) posApos = text.IndexOf(APOS, pos + 1);
            } else {
                sb.Append(APOS);
                sb.Append(text.Substring(pos, posApos - pos));
                sb.Append(APOS);
                pos = posApos;
                if (loop) posQuote = text.IndexOf(QUOTE, pos + 1);
            }//if
    
            // Toggle
            containsApos = !containsApos;
    
        }//while
    
        sb.Append(")");
    
        return sb.ToString();
    
    }//method
    

提交回复
热议问题