我想转换这个 xml:
<Root>
<Result>
<Message>
<Header>
<!-- Hundreds of child nodes -->
</Header>
<Body>
<Node1>
<!-- Hundreds of child nodes -->
<Node2>
<!-- Hundreds of child nodes -->
<Node3>
<!-- Hundreds of child nodes -->
<NodeX>value 1 to be changed</NodeX>
<!-- Hundreds of child nodes -->
<Node4>
<!-- Hundreds of child nodes -->
<NodeY>value 2 to be changed</NodeY>
</Node4>
</Node3>
</Node2>
</Node1>
</Body>
<RealValuesRoot>
<!-- These two nodes -->
<Value ID="1">this value must replace the value of Node X</Value>
<Value ID="2">this value must replace the value of Node Y</Value>
</RealValuesRoot>
</Message>
</Result>
<!-- Hundreds to thousands of similar MessageRoot nodes -->
</Root>
进入这个 xml:
<Root>
<Result>
<Message>
<Header>
<!-- Hundreds of child nodes -->
</Header>
<Body>
<Node1>
<!-- Hundreds of child nodes -->
<Node2>
<!-- Hundreds of child nodes -->
<Node3>
<!-- Hundreds of child nodes -->
<NodeX>this value must replace the value of Node X</NodeX>
<!-- Hundreds of child nodes -->
<Node4>
<!-- Hundreds of child nodes -->
<NodeY>this value must replace the value of Node Y</NodeY>
</Node4>
</Node3>
</Node2>
</Node1>
</Body>
</Message>
</Result>
<!-- Hundreds to thousands of similar MessageRoot nodes -->
</Root>
除了以下变化外,输出与输入几乎相同:
“值”节点具有唯一 ID,表示消息正文中的唯一 xpath,例如ID 1 refres to xpath/Message/Body/Node1/Node2/Node3/NodeX.
我必须使用微软的 xslt 1.0 版!!
我已经有一个 xslt 可以正常工作并且可以做我想做的一切,但我对性能不满意!
我的 xslt 工作方式如下:
我创建了一个类似于键值对的全局字符串变量,类似于:1:xpath1_2:xpath2_ … _N:xpathN。此变量将“值”节点的 ID 与消息正文中需要替换的节点相关联。
xslt 从根节点开始递归迭代输入的 xml。
我计算当前节点的 xpath,然后执行以下操作之一:
如前所述,我的 xslt 工作正常,但我想尽可能提高性能,请随时提出一个完整的新 xslt 逻辑!欢迎提出您的想法和建议!
最佳答案
I compute the xpath for the current node then do one of the following...
这可能是您的效率低下 - 如果您每次都重新计算返回根的路径,您可能正在查看 O(N2) 算法。没有看到您的 XSLT,这是相当推测性的,但您可以通过使用参数将当前路径传递给递归来稍微调整一下 - 如果您的主要算法基于标准身份模板
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
</xsl:template>
然后把它改成类似的东西
<xsl:template match="@*|node()">
<xsl:param name="curPath" />
<xsl:copy>
<xsl:apply-templates select="@*|node()">
<xsl:with-param name="curPath" select="concat($curPath, '/', name())" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
或者您构建所需路径的任何逻辑。现在,在您要处理的节点的特定模板中,您已经有了到它们父节点的路径,并且您不必每次都一直走到根节点。
您可能需要添加一个
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
所以你不会在 $curPath 的前面得到双斜线
The xpaths are hardcoded in the xslt as a string in a global variable
如果您在 XML 结构中表示此映射而不是字符串,那么您可以使用 key 机制来加快查找速度:
<xsl:variable name="rtfLookupTable">
<lookuptable>
<val xpath="/first/xpath/expression" id="1" />
<val xpath="/second/xpath/expression" id="2" />
<!-- ... -->
</lookuptable>
</xsl:variable>
<xsl:variable name="lookupTable" select="msxsl:node-set($rtfLookupTable)" />
<xsl:key name="valByXpath" match="val" use="@xpath" />
(将 xmlns:msxsl="urn:schemas-microsoft-com:xslt" 添加到您的 xsl:stylesheet)。或者,如果您想避免使用 node-set 扩展函数,那么另一种定义可能是
<xsl:variable name="lookupTable" select="document('')//xsl:variable[name='rtfLookupTable']" />
其工作原理是将样式表本身视为纯 XML 文档。
跨多个文档的键在 XSLT 1.0 中有点繁琐,但可以做到,本质上你必须在调用键函数之前切换当前上下文以指向 $lookupTable,所以你需要将当前上下文保存在变量中以供您稍后引用:
<xsl:template match="text()">
<xsl:param name="curPath" />
<xsl:variable name="dot" select="." />
<xsl:variable name="slash" select="/" />
<xsl:for-each select="$lookupTable">
<xsl:variable name="valId" select="key('valByXpath', $curPath)/@id" />
<xsl:choose>
<xsl:when test="$valId">
<xsl:value-of select="$slash//Value[@id = $valId]" />
<!-- or however you extract the right Value -->
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$dot" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
或者事实上,为什么不让 XSLT 引擎为您完成这些繁重的工作。而不是将您的映射表示为字符串
/path/to/node1_1:/path/to/node2_2
直接将其表示为模板
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- copy everything as-is apart from exceptions below -->
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
</xsl:template>
<!-- delete the RealValuesRoot -->
<xsl:template match="RealValuesRoot" />
<xsl:template match="/path/to/node1">
<xsl:copy><xsl:value-of select="//Value[id='1']" /></xsl:copy>
</xsl:template>
<xsl:template match="/path/to/node2">
<xsl:copy><xsl:value-of select="//Value[id='2']" /></xsl:copy>
</xsl:template>
</xsl:stylesheet>
我相信您可以看到如何使用某种模板机制(甚至可以是另一种 XSLT)从现有映射轻松自动生成特定模板。
关于xml - 如何使用 Microsoft xslt 1.0 高效地稍微修改大型 xml 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19277390/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t