A lightweight, dependency‑free utility for converting XPath selectors into CSS selectors. Ideal for scrapers, DOM utilities, testing frameworks, and environments where XPath is stored but CSS is executed.
This package is a modern TypeScript rewrite of the original Python cssify converter created by santiycr. It was later ported to JavaScript by Dither, and subsequently converted to ES2015 and CommonJS, and published to npm by svenheden.
npm install --save-dev @miichom/xpath2css// npm, pnpm, yarn, bun
import { xPathToCss } from "@miichom/xpath2css";
// deno
import { xPathToCss } from "https://esm.sh/@miichom/xpath2css";
import { xPathToCss } from "npm:@miichom/xpath2css"; // or directly from npm
const xPath = "//div[@id="foo"][2]/span[@class="bar"]//a[contains(@class, "baz")]//img[1]";
const css = xPathToCss(xPath);
console.log(css) // => 'div#foo:nth-of-type(2) > span.bar a[class*=baz] img:first-of-type'Bug reports, improvements, and new test cases are welcome. See the Contributing Guidelines for details.
MIT © 2015-2026 Jonathan Svenheden, 2026-present miichom