tags:

views:

2257

answers:

6

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's Pizza"]
//review[@name='"Pizza" Pam']

But if you have BOTH e.g [Fred's "Fancy Pizza"] then you have to use something like this Escaping Strings in XPath (C++) to generate

//review[@name=Concat("Fred's ",'"Fancy Pizza"')]

Anyone have a function in c# to do this?

Some links that are close

EDIT: A few answers have suggested escaping ' with ' and " with " but although this makes sense it does not work, try it using the XML fragment

<review name="Bob's Pizza"/>

and the xpath

//review[@name='Bob&apos;s Pizza']
A: 

URL: http://vaibhavgaikwad.wordpress.com/2007/10/04/handling-apostrophe-single-quote-in-xpath-expressions-in-net/

quote:

  public static string GetXPathString(string input) {
    string[] fragments = input.Split(new char[] { ‘\” });
    string result = “”;
    result += “concat(””;
    for (int i = 0; i < fragments.Length; i++)
    {
      result += “, ‘” + fragments[i] + “‘”;
      if (i < fragments.Length - 1)
      {
        result += “, \”‘\”";
      }
    }
    result += “)”;
    return result;
  }

And here is how you modify the above code so as to use our new function: // remember to remove the single-quotes after = and ]

XmlNode n = doc.SelectSingleNode(“/root/emp[@lname=" + GetXPathString(ln) + "]“);

Or just do like the previous post suggested. make an easy func replacing the ' and " with & apos ; and & quot ;

Wolf5
Using ' or " DOES NOT work - honestly, try it! Try and find<review name="Bob's Pizza"/> using xpath with ' encoded as '
Ryan
Also the routine you've quoted only works with single quotes, it will fail if the string contains double quotes.
Ryan
A: 

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 '.

Ryan
+2  A: 

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]");
Robert Rossney
A: 

I was in need of this so I created this solution, for C#.

    /// <summary>
    /// Returns a valid XPath statement to use for searching attribute values regardless of 's or "s
    /// </summary>
    /// <param name="attributeValue">Attribute value to parse</param>
    /// <returns>Parsed attribute value in concat() if needed</returns>
    public static string GetXpathStringForAttributeValue(string attributeValue)
    {
        bool hasApos = attributeValue.Contains("'");
        bool hasQuote = attributeValue.Contains("\"");

        if (!hasApos)
        {
            return "'" + attributeValue + "'";
        }
        if (!hasQuote)
        {
            return "\"" + attributeValue + "\"";
        }

        StringBuilder result = new StringBuilder("concat(");
        StringBuilder currentArgument = new StringBuilder();
        for (int pos = 0; pos < attributeValue.Length; pos++)
        {
            switch (attributeValue[pos])
            {
                case '\'':
                    result.Append('\"');
                    result.Append(currentArgument.ToString());
                    result.Append("'\",");
                    currentArgument.Length = 0;
                    break;
                case '\"':
                    result.Append('\'');
                    result.Append(currentArgument.ToString());
                    result.Append("\"\',");
                    currentArgument.Length = 0;
                    break;
                default:
                    currentArgument.Append(attributeValue[pos]);
                    break;
            }
        }
        if (currentArgument.Length == 0)
        {
            result[result.Length - 1] = ')';
        }
        else
        {
            result.Append("'");
            result.Append(currentArgument.ToString());
            result.Append("')");
        }
        return result.ToString();
    }
A: 

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("\"", "\",'\"',\""));
  }
 }
tiwahu
A: 

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
Jack