-
Notifications
You must be signed in to change notification settings - Fork 129
Trim the XML output file #321
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
alfsb
wants to merge
7
commits into
php:master
Choose a base branch
from
alfsb:the-big-trim
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
Show all changes
7 commits
Select commit
Hold shift + click to select a range
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
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,193 @@ | ||
| <?php | ||
| // SPDX-License-Identifier: 0BSD | ||
| // © André L F S Bacci <ae#php.net> | ||
| /* | ||
| This script reads a RelaxNG XML file, and calculates all elements | ||
| that _not_ contain <text/> contents, in all alternatives. That is, | ||
| the list shows all elements that can have all inter-element | ||
| whitespace removed, without affecting the expected parsing of the | ||
| XML document that follows the RelaxNG specification. | ||
|
|
||
| If run with a second XML argument, the script will calculate all savings | ||
| that can be done by stripping these insignificant whitespace between | ||
| elements. | ||
|
|
||
| See mentions of 'docbookwsi' on source code for parts that need to be | ||
| updated if/when a new version of Docbook is used. */ | ||
|
|
||
| $argv0 = array_shift( $argv ) ?? null; | ||
| $rngFile = array_shift( $argv ) ?? null; | ||
| $xmlFile = array_shift( $argv ) ?? null; | ||
|
|
||
| if ( $rngFile == null ) | ||
| { | ||
| print "Usage: '$argv0' rngFile [xmlFile]\n\n"; | ||
| return; | ||
| } | ||
|
|
||
| $list = generate_element_trim_list( $rngFile ); | ||
|
|
||
| if ( $xmlFile == null ) | ||
| { | ||
| foreach( $list as $elem => $hasText ) | ||
| if ( ! $hasText ) | ||
| print "$elem\n"; | ||
| exit( 0 ); | ||
| } | ||
| else | ||
| xml_trim_stats( $xmlFile , $list ); | ||
|
|
||
| exit( 0 ); | ||
|
|
||
| function generate_element_trim_list( string $rngFilename ) : array | ||
| { | ||
| $doc = new DOMDocument(); | ||
| if ( ! $doc->load( $rngFilename , LIBXML_NOBLANKS ) ) | ||
| throw new Exception( "XML load failed.\n" ); | ||
|
|
||
| // First, we get all elements definitions that directly | ||
| // mentions <text/>, and also gather all <ref>s they refer. | ||
|
|
||
| $elemText = []; | ||
| $elemRefs = []; | ||
|
|
||
| $xpath1 = new DOMXpath( $doc ); | ||
| $xpath2 = new DOMXpath( $doc ); | ||
| $xpath1->registerNamespace ( 'rng' , 'http://relaxng.org/ns/structure/1.0' ); | ||
| $xpath2->registerNamespace ( 'rng' , 'http://relaxng.org/ns/structure/1.0' ); | ||
|
|
||
| $list = $xpath1->query( '//rng:element' ); | ||
| foreach( $list as $elem ) | ||
| { | ||
| $name = $elem->getAttribute( 'name' ); | ||
| if ( $name == '' ) | ||
| continue; | ||
|
|
||
| $text = count ( $xpath2->query( './/rng:text' , $elem ) ); | ||
| $refs = $xpath2->query( './/rng:ref' , $elem ); | ||
|
|
||
| $elemText[ $name ] = $text; | ||
| $elemRefs[ $name ] = []; | ||
|
|
||
| foreach( $refs as $ref ) | ||
| { | ||
| $refName = $ref->getAttribute( 'name' ); | ||
| $elemRefs[ $name ][] = $refName; | ||
| } | ||
| } | ||
|
|
||
| unset( $xpath1 ); | ||
| unset( $xpath2 ); | ||
|
|
||
| // After all elements are collected, and directly textual elements | ||
| // are marked, we can remove all <element>s, as they cannot influence | ||
| // if a parent element is trimmable or not, and so that any <text/> | ||
| // inside of a <element> cannot be found by XPaths, while exploring | ||
| // the original <element>'s <ref>erences. | ||
|
|
||
| $xpath3 = new DOMXpath( $doc ); | ||
| $xpath3->registerNamespace ( 'rng' , 'http://relaxng.org/ns/structure/1.0' ); | ||
|
|
||
| $todoDels = []; | ||
| $dels = $xpath3->query( '//rng:element' ); | ||
| foreach( $dels as $del ) | ||
| array_push( $todoDels , $del ); | ||
| foreach( $todoDels as $del ) | ||
| $del->parentNode->removeChild( $del ); | ||
|
|
||
| // Then, we explore all references of all elements, for | ||
| // indirect mentions of <text/>s. | ||
|
|
||
| foreach( $elemText as $name => $text ) | ||
| { | ||
| $text = element_references_contains_text( $doc , $name , $elemRefs[ $name ] ); | ||
| $elemText[ $name ] |= $text; | ||
| } | ||
|
|
||
| return $elemText; | ||
| } | ||
|
|
||
| function element_references_contains_text( DOMDocument $doc , string $elemName , array $refs ) : bool | ||
| { | ||
| $ret = false; | ||
| $doneRefs = []; | ||
| $todoRefs = array_unique( $refs ); | ||
|
|
||
| $xpath = new DOMXpath( $doc ); | ||
| $xpath->registerNamespace ( 'rng' , 'http://relaxng.org/ns/structure/1.0' ); | ||
|
|
||
| while ( ( $refName = array_pop( $todoRefs ) ) != null ) | ||
| { | ||
| $doneRefs[ $refName ] = true; | ||
|
|
||
| $defs = $xpath->query( "//rng:define[@name='$refName']" ); | ||
| if ( $defs->count() != 1 ) | ||
| throw new Exception( "Unique define search failed for '$refName'." ); | ||
| $def = $defs[0]; | ||
|
|
||
| $text = count ( $xpath->query( './/rng:text' , $def ) ); | ||
| if ( $text ) | ||
| return true; | ||
|
|
||
| $subRefs = $xpath->query( './/rng:ref' , $def ); | ||
| foreach( $subRefs as $subRef ) | ||
| { | ||
| $subRefName = $subRef->getAttribute( 'name' ); | ||
| if ( isset( $doneRefs[ $subRefName ] ) ) | ||
| continue; | ||
| $todoRefs[] = $subRefName; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| function xml_trim_stats( string $xmlFilename , array $elemText ) | ||
| { | ||
| $doc = new DOMDocument(); | ||
| if ( ! $doc->load( $xmlFilename ) ) | ||
| throw new Exception( "XML load failed.\n" ); | ||
|
|
||
| $stats = []; | ||
| xml_trim_stats_enter( $doc->documentElement , $elemText, $stats ); | ||
| arsort( $stats ); | ||
|
|
||
| $total = 0; | ||
| foreach( $stats as $elem => $trimSize ) | ||
| { | ||
| print "$trimSize $elem\n"; | ||
| $total += $trimSize; | ||
| } | ||
| print "\ntotal $total\n"; | ||
| } | ||
|
|
||
| function xml_trim_stats_enter( DOMNode $node , array $elemText , array & $stats , int $level = 0 ) | ||
| { | ||
| $name = $node->nodeName; | ||
| $text = $elemText[ $name ] ?? true; | ||
|
|
||
| if ( ! $text ) | ||
| { | ||
| $size = 0; | ||
| $dels = []; | ||
|
|
||
| foreach( $node->childNodes as $child ) | ||
| if ( $child->nodeType == XML_TEXT_NODE ) | ||
| if ( trim( $child->nodeValue ) == '' ) | ||
| $dels[] = $child; | ||
|
|
||
| foreach( $dels as $del ) | ||
| { | ||
| $size += strlen( $del->nodeValue ); | ||
| $del->parentNode->removeChild( $del ); | ||
| } | ||
|
|
||
| if ( isset( $stats[ $name ] ) ) | ||
| $stats[ $name ] += $size; | ||
| else | ||
| $stats[ $name ] = $size; | ||
| } | ||
|
|
||
| foreach( $node->childNodes as $child ) | ||
| xml_trim_stats_enter( $child , $elemText , $stats , $level + 1 ); | ||
| } | ||
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.
What parameters did you use to test this and determine the list? Perhaps we should document that.
I tried it using this, and it seems to work fine.