-
Notifications
You must be signed in to change notification settings - Fork 24
Add <product> element validation for SPS 1.10 compliance #1134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
3
commits into
master
Choose a base branch
from
copilot/add-validations-for-product-element
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| """ | ||
| Model for extracting <product> elements from XML documents. | ||
|
|
||
| <product> is used for marking book review references when related to a book | ||
| or book chapter. It should appear in articles with @article-type="book-review". | ||
|
|
||
| Example: | ||
| <product product-type="book"> | ||
| <person-group person-group-type="author"> | ||
| <name> | ||
| <surname>ONFRAY</surname> | ||
| <given-names>Michel</given-names> | ||
| </name> | ||
| </person-group> | ||
| <source>La comunidad filosófica</source> | ||
| <publisher-name>Gedisa</publisher-name> | ||
| <year>2008</year> | ||
| </product> | ||
| """ | ||
|
|
||
|
|
||
| class ArticleProducts: | ||
| """ | ||
| Extracts all <product> elements from an XML article document. | ||
|
|
||
| Processes the main article's <article-meta> to extract product information. | ||
| """ | ||
|
|
||
| def __init__(self, xmltree): | ||
| """ | ||
| Initialize with XML tree. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| xmltree : lxml.etree._Element | ||
| The root element of the XML document | ||
| """ | ||
| self.xmltree = xmltree | ||
|
|
||
| @property | ||
| def article_type(self): | ||
| """Returns the article-type attribute from the root <article> element.""" | ||
| return self.xmltree.get("article-type") | ||
|
|
||
| @property | ||
| def article_lang(self): | ||
| """Returns the xml:lang attribute from the root <article> element.""" | ||
| return self.xmltree.get("{http://www.w3.org/XML/1998/namespace}lang") | ||
|
|
||
| @property | ||
| def products(self): | ||
| """ | ||
| Extract all <product> elements from article-meta. | ||
|
|
||
| Yields | ||
| ------ | ||
| dict | ||
| Dictionary containing product information: | ||
| - product_type: Value of @product-type attribute | ||
| - source: Text content of <source> element | ||
| - has_author: Whether <person-group person-group-type="author"> exists | ||
| - has_publisher_name: Whether <publisher-name> exists | ||
| - has_year: Whether <year> exists | ||
| - person_groups: List of person-group types found | ||
| - isbn: Text content of <isbn> element | ||
| - publisher_loc: Text content of <publisher-loc> element | ||
| - size: Text content of <size> element | ||
| - parent: "article" | ||
| - parent_id: None | ||
| - parent_article_type: Article type attribute | ||
| - parent_lang: Article language attribute | ||
| """ | ||
| article_type = self.article_type | ||
| article_lang = self.article_lang | ||
|
|
||
| for product in self.xmltree.xpath(".//front/article-meta/product"): | ||
| product_type = product.get("product-type") | ||
|
|
||
| source_elem = product.find("source") | ||
| source = None | ||
| if source_elem is not None: | ||
| source = (source_elem.text or "").strip() | ||
|
|
||
| person_groups = [ | ||
| pg.get("person-group-type") | ||
| for pg in product.findall("person-group") | ||
| ] | ||
|
|
||
| has_author = any( | ||
| pg.get("person-group-type") == "author" | ||
| for pg in product.findall("person-group") | ||
| ) | ||
|
|
||
| publisher_name_elem = product.find("publisher-name") | ||
| has_publisher_name = ( | ||
| publisher_name_elem is not None | ||
| and bool((publisher_name_elem.text or "").strip()) | ||
| ) | ||
|
|
||
| year_elem = product.find("year") | ||
| has_year = year_elem is not None and bool((year_elem.text or "").strip()) | ||
|
|
||
| isbn_elem = product.find("isbn") | ||
| isbn = (isbn_elem.text or "").strip() if isbn_elem is not None else None | ||
|
|
||
|
Comment on lines
+94
to
+105
|
||
| publisher_loc_elem = product.find("publisher-loc") | ||
| publisher_loc = ( | ||
| (publisher_loc_elem.text or "").strip() | ||
| if publisher_loc_elem is not None | ||
| else None | ||
| ) | ||
|
|
||
| size_elem = product.find("size") | ||
| size = (size_elem.text or "").strip() if size_elem is not None else None | ||
|
|
||
| yield { | ||
| "product_type": product_type, | ||
| "source": source, | ||
| "has_author": has_author, | ||
| "has_publisher_name": has_publisher_name, | ||
| "has_year": has_year, | ||
| "person_groups": person_groups, | ||
| "isbn": isbn, | ||
| "publisher_loc": publisher_loc, | ||
| "size": size, | ||
| "parent": "article", | ||
| "parent_id": None, | ||
| "parent_article_type": article_type, | ||
| "parent_lang": article_lang, | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Text extraction uses
.textfor<source>, which drops mixed content (e.g.,<source>Title <italic>Part</italic></source>). This can incorrectly flag valid<source>as missing/empty. Prefer extracting withnode_plain_text(source_elem)/"".join(source_elem.itertext())so inline markup is preserved before.strip().