我有这个 xml:
<products>
<product>
<name>ad</name>
<category>something</category>
<path>something</path>
<size>S</size>
<color-code>87-3</color-code>
<size-code>24294</size-code>
<size-qty>10</size-qty>
<size-codeproducer>5902228002604</size-codeproducer>
<size>M</size>
<color-code>87-4</color-code>
<size-code>24295</size-code>
<size-qty>64</size-qty>
<size-codeproducer>5902228002611</size-codeproducer>
<size>L</size>
<color-code>87-5</color-code>
<size-code>24296</size-code>
<size-qty>46</size-qty>
<size-codeproducer>5902228002628</size-codeproducer>
<size>XXL</size>
<color-code>87-7</color-code>
<size-code>24298</size-code>
<size-qty>0</size-qty>
<size-codeproducer>5902228002635</size-codeproducer>
<imgs>
<img>pictures/large/7/8/87_2.jpg</img>
<img>pictures/large/7/8/87_1.jpg</img>
<img>pictures/large/7/8/87_4.jpg</img>
<img>pictures/large/7/8/87_5.jpg</img>
<img>pictures/large/7/8/87_3.jpg</img>
<img>pictures/large/7/8/87_6.jpg</img>
</imgs>google.com</url>
<price>7.98</price>
<brand>NIKE</brand>
<color>black</color>
<gender>Man</gender>
</product>
<product>
...
...
...
</product>
</products>
我需要的:
<products>
<product>
<name>ad</name>
<category>something</category>
...
<variation>
<size>S</size>
<color-code>87-3</color-code>
<size-code>24294</size-code>
<size-qty>10</size-qty>
<size-codeproducer>5902228002604</size-codeproducer>
</variation>
<variation>
<size>M</size>
<color-code>87-4</color-code>
<size-code>24295</size-code>
<size-qty>64</size-qty>
<size-codeproducer>5902228002611</size-codeproducer>
</variation>
<variation>
<size>L</size>
<color-code>87-5</color-code>
<size-code>24296</size-code>
<size-qty>46</size-qty>
<size-codeproducer>5902228002628</size-codeproducer>
</variation>
<variation>
<size>XXL</size>
<color-code>87-7</color-code>
<size-code>24298</size-code>
<size-qty>0</size-qty>
<size-codeproducer>5902228002635</size-codeproducer>
</variation>
</product>
<product>
...
</product>
</products>
我有这个 xsl:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/products/product">
<xsl:copy>
<xsl:copy-of select="description|id|name|category|path"/>
<xsl:for-each select="/products/product/size">
<variation>
<xsl:variable name="occur" select="position()"/>
<xsl:copy-of select="."/>
<xsl:copy-of select="/products/product/color-code[$occur]"/>
<xsl:copy-of select="/products/product/size-code[$occur]"/>
<xsl:copy-of select="/products/product/size-qty[$occur]"/>
<xsl:copy-of select="/products/product/size-codeproducer[$occur]"/>
</variation>
</xsl:for-each>
<xsl:copy-of select="imgs|url|price|brand|color|gender"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
结果是一个复制的 xml 文件
size-codeproducer, color-code, size-code and size-qty 在每个产品节点。
任何帮助将不胜感激。
我已经尝试过 xsl 复制的各种变体 - for-each 循环和其他东西,但大多数在解析后没有打印任何东西,打印相同的文档或存在其他问题。我猜问题是我正在使用的路径(xpaths?)。
最佳答案
只是为了展示另一种对相邻项进行分组或从 XSLT 1.0 中的内容开始的方法,此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*" name="shallow-copy">
<xsl:copy>
<xsl:apply-templates select="node()[1]|@*"/>
</xsl:copy>
<xsl:apply-templates select="following-sibling::node()[1]"/>
</xsl:template>
<xsl:template match="size">
<variation>
<xsl:call-template name="group-starting-with"/>
</variation>
<xsl:apply-templates select="following-sibling::node()[1]"/>
</xsl:template>
<xsl:template match="color-code|size-code|size-qty|size-codeproducer">
<xsl:apply-templates select="following-sibling::node()[1]"/>
</xsl:template>
<xsl:template match="node()" mode="group-starting-with"
name="group-starting-with">
<xsl:copy>
<xsl:apply-templates select="node()[1]|@*"/>
</xsl:copy>
<xsl:apply-templates select="following-sibling::node()[1]"
mode="group-starting-with"/>
</xsl:template>
<xsl:template match="size|imgs" mode="group-starting-with"/>
</xsl:stylesheet>
输出
<products>
<product>
<name>ad</name>
<category>something</category>
<path>something</path>
<variation>
<size>S</size>
<color-code>87-3</color-code>
<size-code>24294</size-code>
<size-qty>10</size-qty>
<size-codeproducer>5902228002604</size-codeproducer>
</variation>
<variation>
<size>M</size>
<color-code>87-4</color-code>
<size-code>24295</size-code>
<size-qty>64</size-qty>
<size-codeproducer>5902228002611</size-codeproducer>
</variation>
<variation>
<size>L</size>
<color-code>87-5</color-code>
<size-code>24296</size-code>
<size-qty>46</size-qty>
<size-codeproducer>5902228002628</size-codeproducer>
</variation>
<variation>
<size>XXL</size>
<color-code>87-7</color-code>
<size-code>24298</size-code>
<size-qty>0</size-qty>
<size-codeproducer>5902228002635</size-codeproducer>
</variation>
<imgs>
<img>pictures/large/7/8/87_2.jpg</img>
<img>pictures/large/7/8/87_1.jpg</img>
<img>pictures/large/7/8/87_4.jpg</img>
<img>pictures/large/7/8/87_5.jpg</img>
<img>pictures/large/7/8/87_3.jpg</img>
<img>pictures/large/7/8/87_6.jpg</img>
</imgs>
<url>google.com</url>
<price>7.98</price>
<brand>NIKE</brand>
<color>black</color>
<gender>Man</gender>
</product>
<product>... ... ...</product>
</products>
请注意:这是在以下斧头中遍历。这意味着每条规则都负责将模板应用于第一个 child 和第一个后续 sibling 。对于分组,您需要一个具有匹配标准的规则开始,空规则在不匹配标准时停止,并在标准匹配但您不分组时绕过规则。
关于php - 需要借助 xsl/xslt 将重复的 XML 节点放在单独的节点中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55713750/
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我注意到像bundler这样的项目在每个specfile中执行requirespec_helper我还注意到rspec使用选项--require,它允许您在引导rspec时要求一个文件。您还可以将其添加到.rspec文件中,因此只要您运行不带参数的rspec就会添加它。使用上述方法有什么缺点可以解释为什么像bundler这样的项目选择在每个规范文件中都需要spec_helper吗? 最佳答案 我不在Bundler上工作,所以我不能直接谈论他们的做法。并非所有项目都checkin.rspec文件。原因是这个文件,通常按照当前的惯例,只
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121
这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Rubysyntaxquestion:Rational(a,b)andRational.new!(a,b)我正在阅读ruby镐书,我对创建有理数的语法感到困惑。Rational(3,4)*Rational(1,2)产生=>3/8为什么Rational不需要new方法(我还注意到例如我可以在没有new方法的情况下创建字符串)?
我的Gallery模型中有以下查询:media_items.includes(:photo,:video).rank(:position_in_gallery)我的图库模型有_许多媒体项,每个都有一个照片或视频关联。到目前为止,一切正常。它返回所有media_items包括它们的photo或video关联,由media_item的position_in_gallery属性排序。但是我现在需要将此查询返回的照片限制为仅具有is_processing属性的照片,即nil。是否可以进行相同的查询,但条件是返回的照片等同于:.where(photo:'photo.is_processingIS
我需要用任何语言编写一个算法,根据3个因素对数组进行排序。我以度假村为例(如Hipmunk)。假设我想去度假。我想要最便宜的地方、最好的评论和最多的景点。但是,显然我找不到在所有3个中都排名第一的方法。Example(assumingthereare20importantattractions):ResortA:$150/night...98/100infavorablereviews...18of20attractionsResortB:$99/night...85/100infavorablereviews...12of20attractionsResortC:$120/night
修改(澄清问题)我已经花了几天时间试图弄清楚如何从Facebook游戏中抓取特定信息;但是,我遇到了一堵又一堵砖墙。据我所知,主要问题如下。我可以使用Chrome的检查元素工具手动查找我需要的html-它似乎位于iframe中。但是,当我尝试抓取该iframe时,它是空的(属性除外):如果我使用浏览器的“查看页面源代码”工具,这与我看到的输出相同。我不明白为什么我看不到iframe中的数据。答案不是它是由AJAX之后添加的。(我知道这既是因为“查看页面源代码”可以读取Ajax添加的数据,也是因为我有b/c我一直等到我可以看到数据页面之后才抓取它,但它仍然不存在)。发生这种情况是因为
这个问题在这里已经有了答案:HashsyntaxinRuby[duplicate](1个回答)关闭5年前。我有一个Recipe,其中包含以下未通过lint测试的代码:service'apache'dosupports:status=>true,:restart=>true,:reload=>trueend失败并出现错误:UsethenewRuby1.9hashsyntax.supports:status=>true,:restart=>true,:reload=>true不确定新语法是什么样的...有人可以帮忙吗?
我的问题很简单:我是否必须在使用RubyonRails的类上require'csv'?如果我打开一个railsconsole并尝试使用CSVgem它可以工作,但我必须在文件中这样做吗? 最佳答案 CSVlibrary是ruby标准库的一部分;它不是gem(即第三方库)。与所有标准库(与核心库不同)一样,csv不会由ruby解释器自动加载。所以是的,在您的应用程序中某处您确实需要要求它:irb(main):001:0>CSVNameError:uninitializedconstantCSVfrom(irb):1from/Us