in reply to XML Parsing and Xpath confusion

Think of a [] in an XPath as similar to a WHERE clause in an SQL statement, which is to say a filter.

/ADI/Asset/Asset/Metadata suggests that you want some asset metadata nodes. I'm going to assume that's correct for now.

But we only want metadata nodes where AMS/@Asset_Class equals poster. This is a filter.

/ADI/Asset/Asset/Metadata[ AMS/@Asset_Class = "poster" ]

Since this is a nested collection of assets, maybe you want to look any depth.

//Asset/Metadata[ AMS/@Asset_Class = "poster" ]

With the above, the asset name would be found at relative path AMS/@Asset_name. App_Data would return rows of metadata.


Is the asset metadata really what you want, though? If you wanted the whole asset instead of just its metadata (e.g. if you wanted to access the asset's content), you should use one of the following instead:

/ADI/Asset/Asset[ Metadata/AMS/@Asset_Class = "poster" ]
//Asset[ Metadata/AMS/@Asset_Class = "poster" ]

With these, the asset name would be found at relative path Metadata/AMS/@Asset_name. Metadata/App_Data would return rows of metadata. Content/@Value would return the poster image.


As a side note, an example of multiple [] in an XPath,

//Asset[ AMS/@Asset_Class = "package" and AMS/@Asset_Name = "some_pack +age" ]/Asset[ Metadata/AMS/@Asset_Class = "poster" ]

This would return the poster asset for a specific package.

Replies are listed 'Best First'.
Re^2: XML Parsing and Xpath confusion
by naChoZ (Curate) on Jul 08, 2024 at 14:16 UTC

    Thanks ikegami. I was able to use those tips with XML::LibXML to get what I wanted.

    Now I need to figure out why XML::LibXML changed a completely different part of the xml document. There's a description string elsewhere in the document and it converted an html entity (') to a single quote. I already set expand_entities to 0 so I'm not wtf is up with that... ugh... I mean it's technically correct and probably won't break anything, but I'd rather it not touch anything I didn't ask it to touch.

    --
    Andy