English 中文(简体)
摘自xml tag
原标题:Extracting a filed from xml tag

在Xml文档中,我正在寻找“和带;档案:write”和xml文档有一个完整的Xml标签,在标签中,它具有价值。 我正试图用档案名称对Csv档案中提交的价值进行计算。 问题在于,实地(街区和路道)是第2、第3或第4栏,我无法使用截断指挥部。

是否有更好的办法这样做?

find /opt/mortagage/application.xml -type f -exec egrep -ri "<file:write" /dev/null {} + |uniq| sed  /<!--.*-->/d  | sed  /<!--/,/-->/d 

/opt/mortagage/application.xml:              <file:write doc:id="16630" path="${file.location}" doc:name="Save file to directory">
/opt/mortagage/application.xml:                      <file:write doc:name="Write to complete folder" doc:id="18890" path= #["${file.completeLocation}" ++ vars.zipFileName]  config-ref="File_Config_completed">
/opt/mortagage/application.xml:                      <file:write doc:name="Write to complete folder" doc:id="19990" Path= #["${file.completeLocation}" ++ vars.zipFileName]  config-ref="File_Config_completed">

问题回答

一种“更好的方式”是使用专用处理器进行结构化的数据,在这种情况下,指挥线XML处理器可以很容易地这样做。

Using kislyuk/yq:

xq -r  .. | ."file:write"? | arrays[] // . | ."@path", ."@Path" | strings  in.xml

Using mikefarah/yq (完全无视名称空间):

yq -oy  .. | .write? | select(kind == "map") // .[] | ."+@path" // ."+@Path"  in.xml

使用xmlstarlet:

xmlstarlet sel -t -m  //file:write  -v  @path  -v  @Path  -n in.xml

使用libxml/xmllint:

  • xmllint requires to either declare the actual namespaces (which you haven t provided in the sample), or to defect to ignoring them all by resorting to a local-name() check
  • xmllint also doesn t support the string(…) function on multiple matches, so the best it can do is to output full attribute nodes like path="${file.location}". A workaround could be to subsequently use another tool (like awk or sed) to trim them down.
xmllint --xpath  //*[local-name()="write"]/@path | //*[local-name()="write"]/@Path  in.xml |
  sed  s/^.*?="|"$//g  # removes all up to the first =" and a final "

All of them output something like:

${file.location}
#["${file.completeLocation}" ++ vars.zipFileName]
#["${file.completeLocation}" ++ vars.zipFileName]




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签