如何使用XML样式表提取数据?

时间:2017-08-18 06:58:27

标签: xml

我们有xml,需要提取带有补丁的字符串作为字符串

 <output>

    <line index="68">Patch  26030218     : applied on Tue Aug 01 08:22:54 EDT 2017</line>
    <line index="69">Unique Patch ID:  21310870</line>
    <line index="70">   Created on 30 May 2017, 10:51:03 hrs PST8PDT</line>
    <line index="71">   Bugs fixed:</line>
    <line index="72">     26030218, 25423453</line>
    <line index="73"></line>
    <line index="74">Patch  25879656     : applied on Tue Aug 01 08:22:50 EDT 2017</line>
    <line index="75">Unique Patch ID:  21310870</line>
    <line index="76">   Created on 10 May 2017, 06:43:45 hrs PST8PDT</line>
    <line index="77">   Bugs fixed:</line>
    <line index="78">     20299015, 21972320, 22502493, 25369547, 18681862, 24433711, 19727057</line>
    <line index="79">     21352646, 25879656, 23177648, 18139690, 20803583, 17343514, 19271443</line>
    <line index="80">     19854503, 17551709</line>
  </output>

我们的输出将是

 <line index="68">Patch  26030218     : applied on Tue Aug 01 08:22:54 EDT 2017</line>
   <line index="74">Patch  25879656     : applied on Tue Aug 01 08:22:50 EDT 2017</line>

我使用它作为xml代码样式表,但没有得到输出

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

  <xsl:template match="@*|node()">
 <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
    </xsl:template>

  <xsl:template match="/catalog/cd/line[index='1']>
 <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
    </xsl:template>
</body>
</html>

enter image description here

1 个答案:

答案 0 :(得分:1)

/catalog/cd/line[index='1']的匹配与示例输入中的任何内容都不匹配。这是完全不同的复制/粘贴吗?

尝试类似:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/catalog/cd">
    <xsl:copy-of select="line[starts-with(normalize-space(),'patch')]"/>
  </xsl:template>

</xsl:stylesheet>