{"id":13649539,"url":"https://github.com/gezp/xmacro","last_synced_at":"2025-04-22T14:31:58.317Z","repository":{"id":62589993,"uuid":"415468168","full_name":"gezp/xmacro","owner":"gezp","description":"xmacro is a simple tool to define and parse XML macro.","archived":false,"fork":false,"pushed_at":"2022-09-18T09:58:04.000Z","size":57,"stargazers_count":13,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-12T12:55:44.755Z","etag":null,"topics":["xacro","xml"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gezp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-10-10T02:32:58.000Z","updated_at":"2025-04-07T11:49:27.000Z","dependencies_parsed_at":"2022-11-03T17:56:05.564Z","dependency_job_id":null,"html_url":"https://github.com/gezp/xmacro","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gezp%2Fxmacro","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gezp%2Fxmacro/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gezp%2Fxmacro/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gezp%2Fxmacro/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gezp","download_url":"https://codeload.github.com/gezp/xmacro/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250259078,"owners_count":21401037,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["xacro","xml"],"created_at":"2024-08-02T02:00:18.435Z","updated_at":"2025-04-22T14:31:53.308Z","avatar_url":"https://github.com/gezp.png","language":"Python","funding_links":[],"categories":["Simulation"],"sub_categories":["Extracting data"],"readme":"# xmacro\n![PyPI](https://img.shields.io/pypi/v/xmacro)  ![](https://img.shields.io/pypi/l/xmacro) ![](https://img.shields.io/pypi/dm/xmacro)\n\n`xmacro` is a simple tool to define and parse XML macro. it's inspired by [ros/xacro](https://github.com/ros/xacro) which is an XML macro language desiged for `urdf`. `xmacro` looks like a simplified version of `ros/xacro`, it's simpler, but it works well for xml files like `urdf` and `sdf`). in addition it's flexible, and also easy to use.\n\n* `xmacro` is independent of ROS, you could install it by `pip` .\n* XML namespace isn't used in `xmacro`, there are some reserved tags: `xmacro_include`, `xmacro_define_value`, `xmacro_define_block`, `xmacro_block`.\n* it provides python api so that we could parse xml file in ROS2 launch file.\n\n## Usage\n\nInstallation\n\n```bash\n# install by pip\npip install xmacro\n# install from source code\ngit clone https://github.com/gezp/xmacro.git\ncd xmacro \u0026\u0026 sudo python3 setup.py install\n```\nexamples\n\n```bash\n# some examples in folder test/xmacro\nxmacro test_xmacro_block.xml.xmacro \u003e test_xmacro_block.xml\n```\n\n##  XMLMacro Features\n\n* Value macro\n* Block macro\n* Math expressions\n* Include\n* Python API\n\n### Value macro\n\nValue macro are named values that can be inserted anywhere into the XML document **except `\u003cxmacro_define_block\u003e` block**.\n\nxmacro definition\n\n```xml\n\u003c!--definition of properties --\u003e\n\u003cxmacro_define_value name=\"radius\" value=\"4.3\" /\u003e\n\u003c!--use of properties--\u003e\n\u003ccircle diameter=\"${2 * radius}\" /\u003e\n```\n\ngenerated xml\n\n```xml\n\u003ccircle diameter=\"8.6\" /\u003e\n```\n\n### Block macro\n\nDefine block macros with the macro tag `\u003cxmacro_define_block\u003e`, then specify the macro name and a list of parameters. The list of parameters should be whitespace separated. \n\nThe usage of block macros is to define `\u003cxmacro_block\u003e` which will be replaced with corresponding `\u003cxmacro_define_block\u003e` block.\n\nxmacro definition\n\n```xml\n\u003c!--definition of macro--\u003e\n\u003cxmacro_define_value name=\"mass\" value=\"0.2\" /\u003e\n\u003cxmacro_define_block name=\"box_inertia\" params=\"m x y z\"\u003e\n    \u003cmass\u003e${m}\u003c/mass\u003e\n    \u003cinertia\u003e\n         \u003cixx\u003e${m*(y*y+z*z)/12}\u003c/ixx\u003e\n         \u003cixy\u003e0\u003c/ixy\u003e\n         \u003cixz\u003e0\u003c/ixz\u003e\n         \u003ciyy\u003e${m*(x*x+z*z)/12}\u003c/iyy\u003e\n         \u003ciyz\u003e0\u003c/iyz\u003e\n        \u003cizz\u003e${m*(x*x+z*z)/12}\u003c/izz\u003e\n    \u003c/inertia\u003e\n\u003c/xmacro_define_block\u003e\n\u003c!--use of macro--\u003e\n\u003cinertial\u003e\n     \u003cpose\u003e0 0 0.02 0 0 0\u003c/pose\u003e\n     \u003cxmacro_block name=\"box_inertia\" m=\"${mass}\" x=\"0.3\" y=\"0.1\" z=\"0.2\"/\u003e\n\u003c/inertial\u003e\n```\n\ngenerated xml\n\n```xml\n\u003cinertial\u003e\n    \u003cpose\u003e0 0 0.02 0 0 0\u003c/pose\u003e\n    \u003cmass\u003e0.2\u003c/mass\u003e\n    \u003cinertia\u003e\n        \u003cixx\u003e0.0008333333333333335\u003c/ixx\u003e\n        \u003cixy\u003e0\u003c/ixy\u003e\n        \u003cixz\u003e0\u003c/ixz\u003e\n        \u003ciyy\u003e0.002166666666666667\u003c/iyy\u003e\n        \u003ciyz\u003e0\u003c/iyz\u003e\n        \u003cizz\u003e0.002166666666666667\u003c/izz\u003e\n    \u003c/inertia\u003e\n\u003c/inertial\u003e\n```\n\n* only support simple parameters (string and number), and block parameters isn't supported.\n* it's supported to use other  `xmacro_block`  in `xmacro_define_block` which is recursive definition (the max nesting level is 5).\n\ncondition block\n\na example here (you could find more examples in `test/xmacro/test_xmacro_condition.xml.xmacro`)\n```xml\n\u003c!--use of macro--\u003e\n\u003cinertial\u003e\n    \u003cpose\u003e0 0 0.02 0 0 0\u003c/pose\u003e\n    \u003cxmacro_block name=\"box_inertia\" m=\"${mass}\" x=\"0.3\" y=\"0.1\" z=\"0.2\" condition=\"${mass==0.2}\"/\u003e\n\u003c/inertial\u003e\n```\n* the `condition` can be `True`, `False`, `1`, `0`, we can also use math expression to define condition, but operator `\u003c` and `\u003e` isn't supported in math expression.\n* if `condition` is `False` or `0`, the `xmacro_block` wou't be loaded.\n* `condition` is reserved attribute of `\u003cxmacro_block\u003e`, so `condition` can't be used as `params` of `\u003cxmacro_define_block\u003e`.\n\n### Math expressions\n\n* within dollared-braces `${xxxx}`, you can also write simple math expressions.\n* refer to examples of  **Value macro** and **Block macro** \n* it's implemented by calling `eval()` in python, so it's unsafe for some cases.\n\n### Including other xmacro files\n\nYou can include other xmacro files by using the `\u003cxmacro_include\u003e` tag.\n\n*  it will include the xmcaro definition with tag `\u003cxmacro_define_value\u003e` and macros with tag `\u003cxmacro_define_block\u003e`.\n\n```xml\n\u003cxmacro_include uri=\"file://simple_car/model.sdf.xmacro\"/\u003e\n```\n\n* The uri for `file` means to open the file directly.\n  *  it try to open the file with relative path `simple_car/model.sdf.xmacro` . \n  * you can also try to open file with absolute path `/simple_car/model.sdf.xmacro` with uri `file:///simple_car/model.sdf.xmacro`.\n* `\u003cxmacro_include\u003e` supports to include  recursively.\n\n### Python API\n\nyou can use `xmacro` in python easily\n\n```python\nfrom xmacro.xmacro import XMLMacro\n\nxmacro=XMLMacro()\n#case1 parse from file\nxmacro.set_xml_file(inputfile)\nxmacro.generate()\nxmacro.to_file(outputfile)\n\n#case2 parse from xml string\nxmacro.set_xml_string(xml_str)\nxmacro.generate()\nxmacro.to_file(outputfile)\n\n#case3 generate to string\nxmacro.set_xml_file(inputfile)\nxmacro.generate()\nxmacro.to_string()\n\n#case4 custom macro value\nxmacro.set_xml_file(inputfile)\n# use custom dictionary to overwrite global macro value defined by \u003cxmacro_define_value\u003e\nkv={\"rplidar_a2_h\":0.8}\nxmacro.generate(kv)\nxmacro.to_file(outputfile)\n```\n\n## Maintainer and License \n\nmaintainer : Zhenpeng Ge, zhenpeng.ge@qq.com\n\n`xmacro` is provided under MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgezp%2Fxmacro","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgezp%2Fxmacro","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgezp%2Fxmacro/lists"}