I looked at some other articles here and elsewhere but nothing was exactly what I’m trying to implement.
Please take a look at this xpath query (using mozxpath.js file to clone IE functionality)
var XMLObj.selectSingleNode("/Catalog/Albums/Album/Tracks/Track[../../AlbumNo='" + AlbumNo + "' and ./TrackNo = '" + TrackNo + "']");
How can I convert this expression to jquery?
I tried the following to get me started and it only selects the AlbumNo node:
$(XMLObj).find("AlbumNo:contains(" + AlbumNo + ")", "TrackNo:contains(" + TrackNo + ")");
The xpath expression is looking for the Track node where both conditions are met. How can I rewrite the xpath expression to something jQuery friendly?
Thank you.
Edit
Here is the dumbed-down version of the XML structure:
<Catalog>
<Albums>
<Album>
<AlbumNo>1</AlbumNo>
<Tracks>
<Track>
<TrackNo>1</TrackNo>
<Track>
<TrackNo>2</TrackNo>
<Track>
<TrackNo>3</TrackNo>
<Track>
<TrackNo>4</TrackNo>
<Track>
<TrackNo>5</TrackNo>
<Track>
<TrackNo>6</TrackNo>
<Track>
<TrackNo>7</TrackNo>
<Track>
<TrackNo>8</TrackNo>
<Track>
<TrackNo>9</TrackNo>
<Track>
<TrackNo>10</TrackNo>
<Track>
<TrackNo>11</TrackNo>
<Track>
<TrackNo>12</TrackNo>
</Tracks>
</Album>
</Album>
</Catalog>
An example of the XML structure would be helpful.
I don’t think you can achieve the same with a single selector, you have to use a more complex structure:
It would definitely be easier if the numbers were attributes of the elements and not descendants.
The reason why I use another
filterfunction instead of:containsis that it is the only way you can search for an exact match.:containswill also match if the text you search is only a substring. That is, if you search for track number1,:containswould also match track number10,11,12, etc.If you have to do similar things quite often, it might be useful to extend jQuery with a function which filters elements with certain subelements with certain content.
For example:
which can be used as