使用xslt转换xml;根据值迭代孩子

时间:2016-07-12 15:54:42

标签: xml xslt

我输入的xml是:

    <?xml version="1.0" encoding="UTF-8"?>
<a>
    <e class="object">
        <children class="array">
            <e class="object">
                <children class="array">
                    <e class="object">
                        <id type="number">3</id>
                        <leaf type="boolean">true</leaf>
                        <node type="string">0+0+0</node>
                        <o type="number">18265</o>
                        <text type="string">Bridge Calibration</text>
                        <url type="string">59739.htm</url>
                    </e>
                    <e class="object">
                        <id type="number">4</id>
                        <leaf type="boolean">true</leaf>
                        <node type="string">0+0+1</node>
                        <o type="number">18266</o>
                        <text type="string">External key pad</text>
                        <url type="string">59740.htm#o18266</url>
                    </e>
                </children>
                <id type="number">2</id>
                <node type="string">0+0</node>
                <o type="number">59738</o>
                <text type="string">Specific Test.Lab Combined Modes add-in</text>
                <url type="string">59738.htm</url>
            </e>
        </children>
        <id type="number">1</id>
        <node type="string">0</node>
        <o type="number">61136</o>
        <text type="string">The LMS Test.Lab Combined Modes workbook</text>
        <url type="string">59737.htm</url>
    </e>
</a>

我希望输出为:

<?xml version="1.0" encoding="UTF-8"?>

<topicMap id="TheLMSTest.LabCombinedModesworkbook">
   <title>The LMS Test.Lab Combined Modes workbook</title>
          <topicRef>
      <metadata>
         <topicInformation/>
      </metadata>
      <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
                  href="libraryPath:LMS-import-en_US/topicLibrary/D59738.xml"/>
   </topicRef>
   <topicRef>
      <metadata>
         <topicInformation/>
      </metadata>
      <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
                  href="libraryPath:LMS-import-en_US/topicLibrary/D59739.xml"/>
   </topicRef>
   </topicMap>

所以,主要观点是:

  1. 我想按输入id值的顺序输出topicref信息。对应于id = 2的信息应首先出现,依此类推。 id = 1对应的信息应该用于topicMap和title元素。
  2. 我想要xi:include href按照url包含值,但只包含那些在值中没有#的url元素。
  3. 我理解这个问题可能不太清楚。如果需要,请随时询问更多信息。

1 个答案:

答案 0 :(得分:0)

如果(!)我理解正确,你想做类似的事情:

XSLT 1.0

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

<xsl:template match="/">
    <xsl:variable name="first-obj" select="//e[id = 1]" />
    <topicMap id="{$first-obj/text}">
        <title>
            <xsl:value-of select="$first-obj/text"/>
        </title>
        <xsl:for-each select="//e[id > 1][not(contains(url, '#'))]">
            <topicRef>
                <metadata>
                    <topicInformation/>
                </metadata>
                <xi:include href="libraryPath:LMS-import-en_US/topicLibrary/{url}"/>
            </topicRef>     
        </xsl:for-each>
    </topicMap>
    <xsl:copy>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

应用于您的输入示例,这将返回:

<?xml version="1.0" encoding="UTF-8"?>
<topicMap xmlns:xi="http://www.w3.org/2001/XInclude" id="The LMS Test.Lab Combined Modes workbook">
   <title>The LMS Test.Lab Combined Modes workbook</title>
   <topicRef>
      <metadata>
         <topicInformation/>
      </metadata>
      <xi:include href="libraryPath:LMS-import-en_US/topicLibrary/59738.htm"/>
   </topicRef>
   <topicRef>
      <metadata>
         <topicInformation/>
      </metadata>
      <xi:include href="libraryPath:LMS-import-en_US/topicLibrary/59739.htm"/>
   </topicRef>
</topicMap>

我没有发现应该从给定输入生成"TheLMSTest.LabCombinedModesworkbook"的逻辑。