55
66This guide will help you understand how to configure ` xmlgenerator ` .
77
8+ * [ Basic usage] ( #basic-usage )
9+ * [ Configuration] ( #configuration )
10+ * [ Customizing the generation of elements and values] ( #customizing-the-generation-of-elements-and-values )
11+ * [ Probability of adding optional attributes] ( #probability-of-adding-optional-attributes )
12+ * [ Limiting the number of elements] ( #limiting-the-number-of-elements )
13+ * [ Limiting string length] ( #limiting-string-length )
14+ * [ Limiting the range for numeric values] ( #limiting-the-range-for-numeric-values )
15+ * [ Overriding values] ( #overriding-values )
16+ * [ By tag/attribute name] ( #by-tagattribute-name )
17+ * [ By regular expression for tag/attribute name] ( #by-regular-expression-for-tagattribute-name )
18+ * [ Using built-in functions to generate values] ( #using-built-in-functions-to-generate-values )
19+ * [ Declaring and using local and global variables] ( #declaring-and-using-local-and-global-variables )
20+ * [ Configuring output filenames] ( #configuring-output-filenames )
21+ * [ Applying settings for groups of documents] ( #applying-settings-for-groups-of-documents )
22+ * [ Configuration Example] ( #configuration-example )
23+ * [ Appendix 1: Configuration File Structure] ( #appendix-1-configuration-file-structure )
24+ * [ Appendix 2: Placeholder Functions] ( #appendix-2-placeholder-functions )
25+
26+
827## Basic usage
928
1029Let's start with the simplest scenario: generating an XML document from a single XSD schema and printing the result to the console.
@@ -65,7 +84,6 @@ To demonstrate, we will use the [order.xsd](examples/order.xsd) schema, which de
6584``` xml
6685<?xml version =" 1.0" encoding =" UTF-8" ?>
6786<xs : schema xmlns : xs =" http://www.w3.org/2001/XMLSchema" >
68-
6987 <xs : element name =" order" >
7088 <xs : complexType >
7189 <xs : sequence >
@@ -103,7 +121,6 @@ To demonstrate, we will use the [order.xsd](examples/order.xsd) schema, which de
103121 <xs : attribute name =" status" type =" xs:string" use =" optional" />
104122 </xs : complexType >
105123 </xs : element >
106-
107124</xs : schema >
108125```
109126
@@ -266,7 +283,7 @@ You can set a value for a field by specifying its exact name.
266283``` yaml
267284global :
268285 value_override :
269- # A value of "31" will be set for all <age> elements
286+ # A value of "31" will be set for all <age> elements and attributes
270287 age : " 31"
271288` ` `
272289
@@ -317,7 +334,7 @@ xmlgenerator -c config.yml --pretty examples/employee.xsd
317334</employee >
318335```
319336
320- #### Using built-in functions
337+ #### Using built-in functions to generate values
321338
322339The most powerful method is to use placeholders like ` {{ function }} ` to substitute data generated by built-in functions.
323340
@@ -350,6 +367,29 @@ xmlgenerator -c config.yml --pretty examples/employee.xsd
350367
351368* A full list of available functions is provided in [ Appendix 2] ( #appendix-2-placeholder-functions ) * .
352369
370+ #### Declaring and using local and global variables
371+
372+ If you need to reuse a generated value, you can declare variables in the ` variables ` block and reference them using the ` local ` and ` global ` functions.
373+
374+ Values of variables declared in ` variables.global ` are evaluated from the specified expressions once (on first access) and then reused throughout the entire generation process when referenced via ` global('key') ` .
375+
376+ Values of variables declared in ` variables.local ` are evaluated from the specified expressions and then reused within the generation of a single document when referenced via ` local('key') ` .
377+
378+
379+ ** ` config.yml ` configuration:**
380+ ``` yaml
381+ variables :
382+ global :
383+ shared_id : " {{ uuid }}"
384+ local :
385+ document_code : " prefix_{{ source_extracted }}-{{ uuid }}"
386+
387+ global :
388+ value_override :
389+ someAttribute1 : " {{ global('shared_id') }}"
390+ someAttribute2 : " {{ local('document_code') }}"
391+ ` ` `
392+
353393### Configuring output filenames
354394
355395The ` output_filename` parameter allows you to set a template for the names of generated files. This is especially useful when processing multiple schemas where the results need to be saved to a single directory. Placeholders can also be used in the template.
@@ -482,12 +522,27 @@ Thus, the `specific` section provides a powerful mechanism for fine-tuning gener
482522### Appendix 1: Configuration File Structure
483523
484524``` yaml
525+ # Optional block for variables, accessible as `{{ global('name') }}` and `{{ local('name') }}`.
526+ variables :
527+ # variable values are evaluated from the specified expressions once (on first access) and then
528+ # reused on access via `global('name')`.
529+ global :
530+ invoice_id : " {{ uuid }}"
531+ # You can also reference other variables here via local() and global(). Avoid circular dependencies!
532+ batch_name : " {{ global('invoice_id') }}-{{ number(1, 10) }}"
533+ # variable values are evaluated from the specified expressions and then reused within the generation
534+ # of a single document when accessed via `local('name')`.
535+ local :
536+ customer_prefix : " {{ source_extracted }}"
537+ # You can also reference other variables here via local() and global(). Avoid circular dependencies!
538+ customer_id : " {{ local('customer_prefix') }}-{{ uuid }}"
539+
485540# Global settings (apply to all schemas)
486541global :
487542
488543 # Regular expression to extract a substring from the source xsd schema filename.
489544 # The extracted substring can be used via the `source_extracted` function.
490- # The regular expression must contain the group `extracted`.
545+ # The regular expression must contain the named group `(?P< extracted>...) `.
491546 # Default value: `(?P<extracted>.*).(xsd|XSD)` (extracts the filename without extension).
492547 source_filename : ...
493548
@@ -517,9 +572,6 @@ global:
517572 # Key - string or regular expression to match the tag/attribute name.
518573 # Value - string with optional use of placeholders:
519574 # `{{ function }}` - substitutes the value provided by the predefined function.
520- # `{{ function | modifier }}` - same, but with a modifier [ global | local ].
521- # - `global` - a single value will be used for the entire generation process.
522- # - `local` - a single value will be used within the context of the current document.
523575 #
524576 # The list of available functions is below.
525577 # The order of entries matters; the first matching override will be selected.
@@ -547,6 +599,7 @@ specific:
547599 # override the value set by the global configuration
548600 name_regexp_1 : " static value"
549601 # reset overrides for tags/attributes containing 'name' set by the global configuration
602+ # a random value will be generated
550603 name :
551604` ` `
552605
@@ -560,13 +613,12 @@ Configuration Priority:
560613
561614In the ` value_override` sections, you can specify either a string value or special placeholders:
562615
563- - ` {{ function }}` - Substitutes the value provided by the predefined function.
564- - `{{ function | modifier }}` - Same, but with a modifier `[ global | local ]`, where :
565- - `global` : The function will generate and use *the same single value* throughout the *entire generation process*
566- for all documents.
567- - `local` : The function will generate and use *the same single value* within the scope of *a single generated
568- document*.
569- - No modifier : A new value is generated each time the function is called.
616+ - ` {{ function }}` - substitutes the value provided by the predefined function.
617+ - ` {{ global('name') }}` - gets a value of the predefined global variable `name` (configured in the `variables` block);
618+ the value is generated once per process on first use.
619+ - ` {{ local('name') }}` - gets a value of the predefined local variable `name` (configured in the `variables` block);
620+ the value is recalculated for each generated document. Built-in context values such as `root_element`,
621+ ` source_filename` , `source_extracted`, and `output_filename` are also available through `local()`.
570622
571623**List of Placeholder Functions:**
572624
@@ -576,6 +628,8 @@ In the `value_override` sections, you can specify either a string value or speci
576628| `source_extracted` | A string extracted from the source XSD filename using the regex specified in `source_filename`. |
577629| `output_filename` | String defined by the `output_filename` configuration parameter. |
578630| `root_element` | The name of the root element of the XML document. |
631+ | `local('name')` | Value of the local variable `name` (configured under `variables.local` or built-in context values). |
632+ | `global('name')` | Value of the global variable `name` (configured under `variables.global`). |
579633| `uuid` | A random UUIDv4. |
580634| `regex("pattern")` | A random string value matching the specified regular expression. |
581635| `any('A', "B", C)` | A random value from the provided enumeration. |
@@ -602,4 +656,4 @@ In the `value_override` sections, you can specify either a string value or speci
602656| `kpp` | Tax Registration Reason Code (KPP). |
603657| `snils_formatted` | SNILS (Personal Insurance Account Number) formatted as `123-456-789 90`. |
604658
605- \* It's available to set custom locale via `func ("ru_RU")`. Default locale is `en_US`</td></tr>
659+ \* For functions marked with an asterisk, you can specify a locale, for example : ` first_name ("ru_RU")` . The default locale is `en_US`.
0 commit comments