简体   繁体   English

XSLT:拆分和合并XML元素

[英]XSLT: Split and merge XML elements

I'm a beginner in XSLT programming. 我是XSLT编程的初学者。 I've got the task to transform the following xml: 我已经完成了转换以下xml的任务:

<Test>TestA::test1</Test>
<Test>TestA::test2</Test>
<Test>TestB::test3</Test>
<Test>TestB::test4</Test>

The output xml shall look like this: 输出xml如下所示:

<Class id="TestA">
    <Method id="test1"/>
    <Method id="test2"/>
</Class>
<Class id="TestB">
    <Method id="test3"/>
    <Method id="test4"/>
</Class>

The input xml contains the names of CppUnit test cases in C++ style (pattern Class::Method). 输入的xml包含C ++风格的CppUnit测试用例的名称(模式Class :: Method)。 I've tried a lot of different approaches and read myriad of stackoverflow threds, but couldn't find a solution. 我尝试了许多不同的方法,并且阅读了无数的stackoverflow异常,但是找不到解决方案。

I have to solve the problem using XSLT 1.0. 我必须使用XSLT 1.0解决问题。

Thanks in advance, mexl 在此先感谢,mexl

This is basically a grouping problem, to be solved (in XSLT 1.0) by Muenchian grouping with a (very slight) twist. 这基本上是一个分组问题,需要通过(非常轻微)扭曲的Muenchian分组 (在XSLT 1.0中)解决。 However, first your input must have a root element - otherwise it's not an XML document: 但是,首先,您的输入必须具有根元素-否则,它不是XML文档:

<root>
    <Test>TestA::test1</Test>
    <Test>TestA::test2</Test>
    <Test>TestB::test3</Test>
    <Test>TestB::test4</Test>
</root>

With that in place, the following stylesheet: 完成后,以下样式表:

XSLT 1.0 XSLT 1.0

<?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" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="k" match="Test" use="substring-before(., '::')" />

<xsl:template match="/">
    <output>
        <xsl:for-each select="root/Test[count(. | key('k', substring-before(., '::'))[1]) = 1]">
            <Class id="{substring-before(., '::')}">
                <xsl:for-each select="key('k', substring-before(., '::'))">
                     <Method id="{substring-after(., '::')}"/>
                </xsl:for-each>
            </Class>
        </xsl:for-each>
    </output>
</xsl:template>

</xsl:stylesheet>

will return: 将返回:

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <Class id="TestA">
      <Method id="test1"/>
      <Method id="test2"/>
   </Class>
   <Class id="TestB">
      <Method id="test3"/>
      <Method id="test4"/>
   </Class>
</output>

Use a combination of xsl:key / generate-id , substring-before /substring-after , as follows: 结合使用xsl:key / generate-idsubstring-before /substring-after ,如下所示:

Given 特定

<Tests>
  <Test>TestA::test1</Test>
  <Test>TestA::test2</Test>
  <Test>TestB::test3</Test>
  <Test>TestB::test4</Test>
</Tests>

Use this template: 使用此模板:

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

<xsl:output indent="yes" method="xml" encoding="UTF-8"/>

<xsl:key name="testClassKey" match="Test" use="substring-before(., '::')"/>

<xsl:template match="Test" mode="asMethod">
  <xsl:variable name="methodId" select="substring-after(., '::')" />

  <Method id="{$methodId}" />
</xsl:template>


<xsl:template match="Test">
  <xsl:variable name="thisTest" select="." />
  <xsl:variable name="classId" select="substring-before(., '::')" />

  <Class id="{$classId}">
    <xsl:apply-templates select="//Test[substring-before(., '::') = $classId]" mode="asMethod"/>
  </Class>
</xsl:template>


<xsl:template match="Tests">
  <TestClasses>
    <xsl:apply-templates select="Test[generate-id(.) = generate-id(key('testClassKey', substring-before(., '::'))[1])]" />
  </TestClasses>
</xsl:template>
</xsl:stylesheet>

To get this result: 要获得此结果:

<?xml version="1.0" encoding="UTF-8"?>
<TestClasses>
  <Class id="TestA">
    <Method id="test1" />
    <Method id="test2" />
  </Class>
  <Class id="TestB">
    <Method id="test3" />
    <Method id="test4" />
  </Class>
</TestClasses>

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

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