给出以下xml:
<items>
<item id="item1">
<description id="desc">
<?RELAPP description="Relative" loc="start"?>
<heading id="h1" level="1">HEADING 1</heading>
<p id="p2" num="1">Paragraph A</p>
<?RELAPP description="Relative" loc="end"?>
<?SUMM description="Summary" loc="start"?>
<heading id="h2" level="1">HEADING 2</heading>
<p id="p3" num="2">Paragraph B</p>
<p id="p4" num="3">Paragraph C</p>
<heading id="h3" level="1">HEADING 3</heading>
<p id="p5" num="4">Paragraph D</p>
<p id="p6" num="5">Paragraph E</p>
<?SUMM description="Summary" loc="end"?>
<?drawings description="Drawings" loc="start"?>
<drawings>
<heading id="h4" level="1">HEADING 4</heading>
<p id="p7" num="6">Paragraph F</p>
<p id="p8" num="7">Paragraph G</p>
</drawings>
<?drawings description="Drawings" loc="end"?>
</description>
</item>
</items>
我试图查看以下文字:
<?SUMM description="Summary" loc="start"?>
和
<?SUMM description="Summary" loc="end"?>
那是:
HEADING 2 Paragraph B Paragraph C HEADING 3 Paragraph D Paragraph E
希望在标题和段落之间有一些分离。
我能想出的最好的xsl是:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/items">
<myItems>
<xsl:apply-templates/>
</myItems>
</xsl:template>
<xsl:template match="item">
<xsl:element name="info">
<xsl:element name="summaryPI">
<xsl:for-each select="description/processing-instruction('SUMM')">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
但它只能让我这样:
<?xml version="1.0" encoding="UTF-8"?>
<myItems>
<info>
<summaryPI>description="Summary" loc="start"description="Summary" loc="end"</summaryPI>
</info>
</myItems>
我应该使用什么规则来获取我想要的文字?我尝试过兄弟姐妹和兄弟姐妹,但是我无法让它发挥作用。我使用的是1.0版本。
答案 0 :(得分:1)
怎么样:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:for-each select="//text()[preceding::processing-instruction('SUMM')[contains(., 'loc="start"')]]
[following::processing-instruction('SUMM')[contains(., 'loc="end"')]] ">
<xsl:value-of select="." />
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
应用于您的输入示例,结果将是:
HEADING 2, Paragraph B, Paragraph C, HEADING 3, Paragraph D, Paragraph E
注意:如果可以假设两个处理指令之间的所有节点都是兄弟节点(就像你的例子中那样),那么通过使用它可以提高效率:
<xsl:for-each select="//*[preceding-sibling::processing-instruction('SUMM')[contains(., 'loc="start"')]]
[following-sibling::processing-instruction('SUMM')[contains(., 'loc="end"')]] ">