简体   繁体   English

有没有办法将参数传递给xml? 或修改它?

[英]Is there a way to pass parameters to an xml? Or modifying it?

I would like to pass a certain parameter to an xml, so instead of being a raw xml with all the values by the creation of it, I'd like to change one with a parameter (a user input, for example). 我想将某个参数传递给xml,因此我不想通过创建它来创建具有所有值的原始xml,而是想用参数(例如用户输入)更改一个。

Ideally, I was looking for something like <title> &param1 </title> and be able later on to pass whatever param I would like, but I guess it cannot be done. 理想情况下,我正在寻找像<title> &param1 </title> ,以后能够通过我想要的任何参数,但我想它无法完成。

So like passing a parameter cannot be done (or at least from what I have searched), I thought about editing the xml after it's created. 因此,无法传递参数(或者至少从我搜索过的内容),我想到了在创建后编辑xml。

I have searched mostly with beautifulsoup, because it is what I want to use (and what I am using). 我主要用beautifulsoup搜索,因为它是我想要使用的(以及我正在使用的)。 This is only a little bit of my project. 这只是我项目的一小部分。 for example this and this are some of my research). 例如这个这个是我的一些研究)。

So this is the function I am trying to do: We have an xml, we find the part we want to edit, and we edit it (I know that to access it, it needs to be an integer pruebaEdit[anyString] is not correct. 所以这就是我想要做的功能:我们有一个xml,我们找到了我们要编辑的部分,然后我们编辑它(我知道要访问它,它需要是一个整数pruebaEdit[anyString]不正确。

def editXMLTest():
    editTest="""<?xml version="1.0" ?>
<books>
  <book>
    <title>moon</title>
    <author>louis</author>
    <price>8.50</price>
  </book>
</books>
    """
    soup =BeautifulSoup(editTest)
    for tag in soup.find_all('title'):
        print (tag.string, '\n')
        #tag.string='invented title'
        editTest[tag]='invented title' #I know it has to be an integer, not a string
    print()
    print(editTest)

My expected output should be in the xml: <title>invented title</title> instead of <title>moon</title> . 我的预期输出应该是xml: <title>invented title</title>而不是<title>moon</title>

Edit: added this to my research 编辑: 将此添加到我的研究中

you have to print the results or soup not original string editTest 你必须打印结果或soup不是原始字符串editTest

for tag in soup.find_all('title'):
    print (tag.string, '\n')
    tag.string='invented title'
print(soup)

Using entity references like &param; 使用像&param;这样的实体引用&param; is the closest thing available in XML itself, but it's not very flexible because the entity expansions are defined in a DTD file rather than being supplied programmatically to the XML parser. XML本身是最接近的东西,但它不是很灵活,因为实体扩展是在DTD文件中定义的,而不是以编程方式提供给XML解析器。 Some parsers (I don't know the Python situation) allow you to supply an EntityResolver which can resolve entity references programmatically, but it wouldn't be my first choice of approach. 一些解析器(我不知道Python情况)允许您提供可以以编程方式解析实体引用的EntityResolver,但它不是我的首选方法。

There are of course templating languages that allow XML to be constructed programmatically. 当然,模板语言允许以编程方式构造XML。 XSLT is the most obvious choice; XSLT是最明显的选择; it probably does a lot more than you need, but that's not necessarily a drawback. 它可能比你需要的要多得多,但这不一定是个缺点。 Some other options are listed at https://en.wikipedia.org/wiki/Comparison_of_web_template_engines -- including a handful for the Python environment. https://en.wikipedia.org/wiki/Comparison_of_web_template_engines中列出了一些其他选项 - 包括一些Python环境。 Unfortunately many of these tools, in my experience, are not particularly well documented or supported, so do your research carefully. 不幸的是,根据我的经验,许多这些工具都没有特别好的文档或支持,所以你的研究也要仔细。

With Python's lxml that can run XSLT 1.0 scripts and also the parsing engine to BeautifulSoup , you can pass parameters to modify XML files as needed. 使用可以运行XSLT 1.0脚本的Python的lxml以及BeautifulSoup的解析引擎,您可以根据需要传递参数来修改XML文件。 Simply set the <xsl:param> in XSLT script and in Python pass value via strparam : 只需在XSLT脚本中设置<xsl:param> ,在Python中通过strparam传递值:

XSLT (save as .xsl file, a special .xml file) XSLT (另存为.xsl文件,一个特殊的.xml文件)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" omit_xml_declaration="no"/>
  <xsl:strip-space elements="*"/>

  <!-- INITIALIZE PARAMETER -->
  <xsl:param name="new_title" /> 

  <!-- IDENTITY TRANSFORM -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- REWRITE TITLE TEXT -->
  <xsl:template match="title">
    <xsl:copy>
      <xsl:value-of select="$new_title"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

Python (see output below as comment) Python (参见下面的输出作为注释)

import lxml.etree as et

txt = '''<books>
           <book>
               <title>moon</title>
               <author>louis</author>
               <price>8.50</price>
             </book>
         </books>'''

# LOAD XSL SCRIPT
xml = et.fromstring(txt)
xsl = et.parse('/path/to/XSLTScript.xsl')
transform = et.XSLT(xsl)

# PASS PARAMETER TO XSLT
n = et.XSLT.strparam('invented title')
result = transform(doc, new_title=n)

print(result)
# <?xml version="1.0"?>
# <books>
#   <book>
#     <title>invented title</title>
#     <author>louis</author>
#     <price>8.50</price>
#   </book>
# </books>

# SAVE XML TO FILE
with open('Output.xml', 'wb') as f:
    f.write(result)

Pyfiddle Demo (be sure to click run and check output) Pyfiddle Demo (确保单击运行并检查输出)

使用<xsl>标记在xml中传递参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM