如何使用XSLT展平此XML

时间:2008-12-04 03:45:28

标签: xml xslt

INPUT

<logs>
<logentry revision="648">
<author>nshmyrev</author> 
<date>2008-09-21T19:43:10.819236Z</date> 
<paths>
<path action="M">/trunk/po/ru.pi</path> 
</paths>
<msg>2008-09-21 Nickolay V. Shmyrev nshmyrev@yandex.ru * ru.po: Updated Russian translation.</msg> 
</logentry>
<logentry revision="647">
<author>ckirbach</author> 
<date>2008-09-21T16:25:58.369324Z</date> 
<paths>
<path action="M">/trunk/po/de.po</path> 
<path action="M">/trunk/po/ChangeLog</path> 
</paths>
<msg>* de.po: Updated German translation.</msg> 
</logentry>
<logs>

<logs>
<LogEntry revision="647" author = "ckirbach" action="M">/trunk/po/de.po</LogEntry>
<LogEntry revision="647" author = "ckirbach" action="M">/trunk/po/ChangeLog</LogEntry>
</logs

此外,我想忽略扩展名为'.pi'的所有路径

2 个答案:

答案 0 :(得分:1)

怎么样

<?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:template match="/logs">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="text()"/>

  <xsl:template match="path[substring(.,string-length()-2)!='.pi']">
    <LogEntry revision="{ancestor::logentry/@revision}"
              author="{preceding::author/text()}"
              action="{@action}">
      <xsl:copy-of select="text()"/>
    </LogEntry>
  </xsl:template>

</xsl:stylesheet>

答案 1 :(得分:1)

Alastair的解决方案产生了错误的结果:产生的“author”属性的值是“nshmyrev”但它必须是:“ckirbach”。

测试以下解决方案是否正常工作。

这是一个解决方案,适用于必须忽略的不同扩展程序(在全局<xsl:param/>中指定)。

这种转变:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:param name="pIgnExt" select="'.pi'"/>
 <xsl:variable name="vExtLen"
      select="string-length($pIgnExt)"/>

    <xsl:template match="logs">
      <logs>
        <xsl:apply-templates select="*/*/path"/>
      </logs>
    </xsl:template>

    <xsl:template match="path">
    <xsl:variable name="vthisLen"
         select="string-length(.)"/>
      <xsl:if test=
        "not(substring(.,$vthisLen -$vExtLen +1)
            =
             $pIgnExt
             )">
        <LogEntry revision="{../../@revision}"
                  author="{../../author}"
                  action="{@action}">
        <xsl:copy-of select="node()"/>
      </LogEntry>
      </xsl:if>
    </xsl:template>
</xsl:stylesheet>

应用于原始XML文档(更正为格式正确!):

<logs>
    <logentry revision="648">
        <author>nshmyrev</author>
        <date>2008-09-21T19:43:10.819236Z</date>
        <paths>
            <path action="M">/trunk/po/ru.pi</path>
        </paths>
        <msg>2008-09-21 Nickolay V. Shmyrev nshmyrev@yandex.ru * ru.po: Updated Russian translation.</msg>
    </logentry>
    <logentry revision="647">
        <author>ckirbach</author>
        <date>2008-09-21T16:25:58.369324Z</date>
        <paths>
            <path action="M">/trunk/po/de.po</path>
            <path action="M">/trunk/po/ChangeLog</path>
        </paths>
        <msg>* de.po: Updated German translation.</msg>
    </logentry>
</logs>

产生想要的结果:

<logs>
   <LogEntry revision="647" author="ckirbach" action="M">/trunk/po/de.po</LogEntry>
   <LogEntry revision="647" author="ckirbach" action="M">/trunk/po/ChangeLog</LogEntry>
</logs>

请注意,这可以用作函数ends-with() 的XPath 1.0中的实现,该函数仅在XPath 2.0中可用。