标准 Muenchian 分组 - XSLT [英] standard Muenchian grouping- XSLT

查看:16
本文介绍了标准 Muenchian 分组 - XSLT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用 XSLT 将输入 xml 转换为其他 xml 的 xsl,如下所述.

I was developing an xsl that converts the input xml to other xml using XSLT as mentioned Below.

逻辑:在输入的xml中,我提到了Vehicle"中的四个子元素vehicle_No、vehicle_Model、Description、color".我想得到一个结果如果vehicle_No,vehicle_Model"与下一个车辆"匹配,那么描述"中的值应该成为元素,然后输出应该看起来像......,描述"的 Muenchian 分组时"vehicle_No, vehicle_Model" 匹配,

Logic: In the input xml, I mention four sub-elements "vehicle_No, vehicle_Model, Description, colour" in "Vehicle". I want to get a result " if "vehicle_No, vehicle_Model" matches with the next "vehicle" then the value in the "description" should become elements and then the output should look like..., Also Muenchian grouping for "Description" when "vehicle_No, vehicle_Model" matches,

示例 O/P:如果匹配

Sample O/P: If Matched

<vehicle>
     <car>
          <color>BLACK</color>
          <hood>RED</hood>
     </car>
</vehicle>

如果不匹配

<vehicle>
     <car>
          <color>BLACK</color>
          <hood>RED</hood>
     </car>
     <cycle>
          <color>violet</color>
     </cycle>
</vehicle>

如果输入 xml 中提到的description"值与vehicle_No,vehicle_Model"不匹配.那么 o/p 应该是这样的

If in case i get a same value for "description" mentioned in input xml and "vehicle_No, vehicle_Model" aren't matches. Then the o/p should look like

<vehicle>
     <car>
          <color>BLACK</color>
          <hood>RED</hood>
     </car>
     <cycle>
          <color>violet</color>
     </cycle>
</vehicle>
<vehicle>
     <car>
          <color>RED</color>
     </car>
</vehicle>

我的输入 XML 文件:

My Input XML File:

<t>
<Vehicle>
    <vehicle_No>123</vehicle_No>
    <vehicle_Model>Audi</vehicle_Model>
    <Description>car.color</Description>
    <colour>BLACK</colour>
</Vehicle>
<Vehicle>
    <vehicle_No>123</vehicle_No>
    <vehicle_Model>Audi</vehicle_Model>
    <Description>car.hood</Description>
    <colour>RED</colour>
</Vehicle>
<Vehicle>
    <vehicle_No>123</vehicle_No>
    <vehicle_Model>BMW</vehicle_Model>
    <Description>Bus.Brakes</Description>
    <colour>steel</colour>
</Vehicle>
<Vehicle>
    <vehicle_No>123</vehicle_No>
    <vehicle_Model>BMW</vehicle_Model>
    <Description>Bus.steering</Description>
    <colour>black</colour>
</Vehicle>
<Vehicle>
    <vehicle_No>234</vehicle_No>
    <vehicle_Model>benz</vehicle_Model>
    <Description>cycle.color</Description>
    <colour>violet</colour>
</Vehicle>
<Vehicle>
    <vehicle_No>345</vehicle_No>
    <vehicle_Model>nissan</vehicle_Model>
    <Description>car.color</Description>
    <colour>RED</colour>
</Vehicle>
</t>

XML 中的预期输出:

Expected Output in XML:

<t>
<Vehicle>
    <car>
        <color>BLACK</color>
        <hood>RED</hood>
    </car>
    <Bus>
        <Brakes>steel</Brakes>
        <Steering>black</Steering>
    </Bus>
    <cycle>
        <color>violet</color>
    </cycle>
</Vehicle>
<vehicle>
    <car>
        <color>RED</color>
    </car>
</vehicle>
</t>

如果问题不清楚,请告诉我.

Please let me know if Question was unclear.

推荐答案

这个 XSLT 1.0 样式表 ...

This XSLT 1.0 style-sheet ...

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:key name="vehicle-by-number" match="Vehicle" use="vehicle_No" /> 
<xsl:key name="vehicle-by-form"   match="Vehicle" use="
  concat( vehicle_No, '-', substring-before(Description,'.'))" /> 

<xsl:template match="/*">
 <xsl:copy>
  <xsl:apply-templates select="Vehicle[
   generate-id(.) = generate-id(key('vehicle-by-number',vehicle_No)[1])]"
   mode="vehicle-group" />
 </xsl:copy>
</xsl:template>

<xsl:template match="Vehicle" mode="vehicle-group">
 <xsl:copy>
   <xsl:apply-templates select="key('vehicle-by-number',vehicle_No)
    [generate-id(.) = generate-id(key('vehicle-by-form',
      concat( vehicle_No, '-', substring-before(Description,'.')))[1])]"
    mode="form-group" />
 </xsl:copy>
</xsl:template>

<xsl:template match="Vehicle" mode="form-group">
 <xsl:element name="{substring-before(Description,'.')}">
   <xsl:for-each select="key('vehicle-by-form',concat( vehicle_No, '-', substring-before(Description,'.')))">
    <xsl:element name="{substring-after(Description,'.')}">
      <xsl:value-of select="colour" /> 
    </xsl:element>   
   </xsl:for-each>   
 </xsl:element>
</xsl:template>

</xsl:stylesheet>

...当应用于您的示例输入时,将产生...

... when applied to your sample input, will produce ...

<?xml version="1.0" encoding="utf-8"?>
<t>
  <Vehicle>
    <car>
      <color>BLACK</color>
      <hood>RED</hood>
    </car>
    <Bus>
      <Brakes>steel</Brakes>
      <steering>black</steering>
    </Bus>
  </Vehicle>
  <Vehicle>
    <cycle>
      <color>violet</color>
    </cycle>
  </Vehicle>
  <Vehicle>
    <car>
      <color>RED</color>
    </car>
  </Vehicle>
</t>

虽然这与您列出的预期输出不完全匹配,但我认为差异是由于您列出的预期输出中的错误造成的.我特别指的是值为'紫罗兰色'的节点的位置.

While this is not an exact match for your listed expected output, I believe that the difference is due to an error in your listed expected output. I refer in particular to the position of the node whose value is 'violet'.

此样式表使用 2 个级别的 muenchian 分组.第一级足够直截了当.它将所有车辆组合在一起,在一个具有相同车辆编号的输出车辆元素下.在 muenchian 的 SO 中的大多数示例中,for-each 用于遍历组成员,而我使用了 apply-templates.意思是一样的.

This stylesheet uses 2 levels of muenchian grouping. The first level is straight-forward enough. It groups all the vehicles together, under one output vehicle element which have the same vehicle number. In most examples in SO of muenchian, a for-each is used to iterate through group members, whereas I have used an apply-templates. It amounts to the same thing.

我有第二个密钥(车辆形式),用于第二级分组.通过形式",我的意思是汽车、公共汽车、自行车等.这对车辆编号和形式的关键组.车辆在以表单命名的输出节点下分组.在这个内部组中,我们发出像钢一样的节点,它们是该组的成员.

I have a second key (vehicle-by-form), for the second level of grouping. By 'form', I mean car, bus, cycle etc. This key groups on both vehicle number and form. Vehicles are grouped under an output node named after the form. And within this inner group, we emit nodes like steel, which are members of the group.

这篇关于标准 Muenchian 分组 - XSLT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆