Encoding XPath Expressions with both single and double quotes

前端 未结 8 1179
孤街浪徒
孤街浪徒 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 07:53

    Wow, you all sure are making this complicated. Why not just do this?

    public static string XpathExpression(string value)
    {
        if (!value.Contains("'"))
            return '\'' + value + '\'';
    
        else if (!value.Contains("\""))
            return '"' + value + '"';
    
        else
            return "concat('" + value.Replace("'", "',\"'\",'") + "')";
    }
    

    .NET Fiddle & test

    0 讨论(0)
  • 2020-11-30 07:56

    This is what I've come up with

    public static string EncaseXpathString(string input)
    {         
        // If we don't have any " then encase string in "
        if (!input.Contains("\""))
            return String.Format("\"{0}\"", input);
    
        // If we have some " but no ' then encase in '
        if (!input.Contains("'"))
            return String.Format("'{0}'", input);
    
        // If we get here we have both " and ' in the string so must use Concat
        StringBuilder sb = new StringBuilder("concat(");           
    
        // Going to look for " as they are LESS likely than ' in our data so will minimise
        // number of arguments to concat.
        int lastPos = 0;
        int nextPos = input.IndexOf("\"");
        while (nextPos != -1)
        {
            // If this is not the first time through the loop then seperate arguments with ,
            if (lastPos != 0)
                sb.Append(",");
    
            sb.AppendFormat("\"{0}\",'\"'", input.Substring(lastPos, nextPos - lastPos));
            lastPos = ++nextPos;
    
            // Find next occurance
            nextPos = input.IndexOf("\"", lastPos);
        }
    
        sb.Append(")");
        return sb.ToString();
    }
    

    Called using something like

    XmlNode node = doc.SelectSingleNode("//review[@name=" + EncaseXpathString("Fred's \"Fancy Pizza\"" + "]")
    

    So we get the following results

    EncaseXpathString("Pizza Shed") == "'Pizza Shed'";
    EncaseXpathString("Bob's pizza") == "\"Bob's Pizza\"";
    EncaseXpathString("\"Pizza\" Pam" == "'\"Pizza\" Pam'";
    EncaseXpathString("Fred's \"Fancy Pizza\"") == "concat(\"Fred's \",'\"',\"Fancy Pizza\",'\"')";
    

    So it's only using concat when its necessary (both " and ' in string)

    The last result show the concat operation is not as short as it could be (see question) but its close enough and anything more optimal would be very complex as you would have to look for matching pairs of " or '.

    0 讨论(0)
  • 2020-11-30 07:59

    I've had problems with all solutions so far. One has extra text sections (e.g. '"' or "'") which breaks what you're looking for. One drops all text after the last quote/dblquote which breaks as well.

    This is a dumb and quick solution from a dumb vb developer:

    Function ParseXpathString(ByVal input As String) As String
        input = Replace(input, "'", Chr(1))
        input = Replace(input, """", Chr(2))
        input = Replace(input, Chr(1), "',""'"",'")
        input = Replace(input, Chr(2), "','""','")
        input = "concat('','" + input + "')"
        Return input
    End Function
    

    Usage (same as previous examples):

    x.SelectNodes("/path[@attr=" & ParseXpathString(attrvalue) & "]")
    
    0 讨论(0)
  • 2020-11-30 07:59

    Another variation...my concat() part is a little lazy, but at least it uses the whole value.

        /// <summary>
        /// Returns an XPath string literal to use for searching attribute values (wraped in apostrophes, quotes, or as a concat function).
        /// </summary>
        /// <param name="attributeValue">Attribute value to encode and wrap.</param>
        public static string CreateXpathLiteral(string attributeValue)
        {
            if (!attributeValue.Contains("\""))
            {
                // if we don't have any quotes, then wrap string in quotes...
                return string.Format("\"{0}\"", attributeValue);
            }
            else if (!attributeValue.Contains("'"))
            {
                // if we have some quotes, but no apostrophes, then wrap in apostrophes...
                return string.Format("'{0}'", attributeValue);
            }
            else
            {
                // must use concat so the literal in the XPath will find a match...
                return string.Format("concat(\"{0}\")", attributeValue.Replace("\"", "\",'\"',\""));
            }
        }
    
    0 讨论(0)
  • 2020-11-30 08:00

    Though it certainly won't work in all circumstances, here's a way to sidestep the problem:

    doc.DocumentElement.SetAttribute("searchName", name);
    XmlNode n = doc.SelectNodes("//review[@name=/*/@searchName]");
    
    0 讨论(0)
  • 2020-11-30 08:10

    I needed to do this in XSLT itself, so came up with the following based on the answers on this page:

    <xsl:template name="escape-string">
      <xsl:param name="string"/>
      <xsl:param name="concat" select="true()"/>
      <xsl:variable name="quote">"</xsl:variable>
      <xsl:variable name="apos">'</xsl:variable>
      <xsl:choose>
        <xsl:when test="not(contains($string, $apos))">'<xsl:value-of select="$string"/>'</xsl:when>
        <xsl:when test="not(contains($string, $quote))">"<xsl:value-of select="$string"/>"</xsl:when>
        <xsl:otherwise>
          <xsl:if test="$concat">concat(</xsl:if>
          <xsl:call-template name="escape-string">
            <xsl:with-param name="string" select="substring-before($string, $apos)"/>
            <xsl:with-param name="concat" select="false()"/>
          </xsl:call-template>
          <xsl:text>, "'", </xsl:text>
          <xsl:call-template name="escape-string">
            <xsl:with-param name="string" select="substring-after($string, $apos)"/>
            <xsl:with-param name="concat" select="false()"/>
          </xsl:call-template>
          <xsl:if test="$concat">)</xsl:if>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
    
    0 讨论(0)
提交回复
热议问题