1

XML:

<Book>
   <Title>blahblah</Title>
   <Title>
    <subtitle>bcdf</subtitle><subtitle>bcdf</subtitle>asdfg
   </Title>
   <Title>
    <subtitle>bcdf</subtitle>jhuk<subtitle>bcdf</subtitle>refsdw
  </Title>
  <Title>
   <subtitle>bcdf</subtitle>fdgfjhdc<subtitle>bcdf</subtitle>
  </Title>
 </Book>

输出结果应该是:

 <Title>blahblah</Title>
 <Title>asdfg</Title>
 <Title>jhukrefsdw</Title>
 <Title>fdgfjhdc</Title>
4

4 回答 4

0

您可以通过其索引选择特定元素

/Book/Title[1]/text() 
于 2013-11-10T14:16:19.143 回答
0

你的问题不是很清楚。看到这个。

XML:

<Book>
 <Title>blahblah</Title>
 <Title>
   <subtitle>bcdf</subtitle>
   <subtitle>bcdf</subtitle>blahblah
 </Title>
 <Title>
  <subtitle>bcdf</subtitle>blah
  <subtitle>bcdf</subtitle>blah
 </Title>
 <Title>
   <subtitle>bcdf</subtitle>blahblah
   <subtitle>bcdf</subtitle>
 </Title>
</Book>

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output omit-xml-declaration="yes" indent="yes" />
   <xsl:strip-space elements="*" />
   <xsl:template match="Book">
      <xsl:value-of select="Title[text() = 'blahblah']" />
   </xsl:template>
</xsl:stylesheet>

输出:

blahblah
于 2013-11-10T14:45:38.403 回答
0
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output omit-xml-declaration="yes" indent="yes" />
   <xsl:strip-space elements="*" />
   <xsl:template match="//Title">
      <Title>
         <xsl:for-each select="text()">
           <xsl:value-of select="." />
         </xsl:for-each>
      </Title>
   </xsl:template>
</xsl:stylesheet>
于 2013-11-10T16:35:56.887 回答
0

这里最简单的方法可能是完全去除subtitle元素。以下样式表:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <!-- identity template - copies everything as-is unless overridden -->
  <xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
  </xsl:template>

  <!-- ignore (i.e. delete) subtitle elements -->
  <xsl:template match="subtitle" />
</xsl:stylesheet>

将产生的输出

<Book>
   <Title>blahblah</Title>
   <Title>
    asdfg
   </Title>
   <Title>
    jhukrefsdw
  </Title>
  <Title>
   fdgfjhdc
  </Title>
 </Book>

如果您想修复空白,则添加第三个模板可能就足够了

<xsl:template match="text()">
  <xsl:value-of select="normalize-space()" />
</xsl:template>

并告诉样式表缩进输出

<xsl:output indent="yes" />

然后会产生

<Book>
  <Title>blahblah</Title>
  <Title>asdfg</Title>
  <Title>jhukrefsdw</Title>
  <Title>fdgfjhdc</Title>
</Book>
于 2013-11-10T18:17:51.917 回答