我有两个标头文件(H1,H2),我使用以下XSL写了图像信息:
我的输入XSL:
<xsl:template match="Body">
<xsl:for-each-group select="*" group-starting-with="h1">
<xsl:if test="not(self::p/img)">
<xsl:variable name="image" select="preceding-sibling::*[1][self::p/img]" />
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h2">
<xsl:choose>
<xsl:when test="self::h2">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<body><xsl:apply-templates select="current-group()[not(self::p/img)] except ."/></body>
</topic>
</xsl:when>
<xsl:otherwise>
<body><xsl:apply-templates select="$image|current-group()[not(self::p/img)]"/></body>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</topic>
</xsl:if>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="p">
<p><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="img">
<xsl:element name="image">
<xsl:if test="@src">
<xsl:attribute name="href"><xsl:value-of select="@src"/></xsl:attribute>
</xsl:if>
</xsl:element>
</xsl:template>
我的第一个输入XML是(该类型的图像不是出现的):
<Body>
<p><img src="https://tneb.com"/></p>
<h1>Taking</h1>
<h2>Blood Pressure?</h2>
<p>second.</p>
</Body>
我的第二个XML文件:(正确生成图像)
<Body>
<p><img src="https://tneb.com"/></p>
<h1>Taking</h1>
<p>first</p>
<h2>Blood Pressure?</h2>
<p>second.</p>
</Body>
生成了第一个XML的输出:
<topic id="topic_1">
<title>Taking</title>
<topic id="topic_2">
<title>Blood Pressure?</title>
<body>
<p>second.</p>
</body>
</topic>
</topic>
预期输出将是:
<topic id="topic_1">
<title>Taking</title>
<body>
<p>
<image href="https://tneb.com"/>
</p>
</body>
<topic id="topic_2">
<title>Blood Pressure?</title>
<body>
<p>second.</p>
</body>
</topic>
</topic>
我必须为这两种输入创建XSL,无论是否具有标签段落。请为此建议编码。
预先感谢
我认为您需要在self:h2
的检查中添加一些逻辑,以便如果它在第一个位置出现(即它在其之前没有p
标签),则首先输出图像
<xsl:when test="self::h2">
<xsl:if test="position() = 1">
<body><xsl:apply-templates select="$image"/></body>
</xsl:if>
尝试此XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="Body">
<xsl:for-each-group select="*" group-starting-with="h1">
<xsl:if test="not(self::p/img)">
<xsl:variable name="image" select="preceding-sibling::*[1][self::p/img]" />
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h2">
<xsl:choose>
<xsl:when test="self::h2">
<xsl:if test="position() = 1">
<body><xsl:apply-templates select="$image"/></body>
</xsl:if>
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<body><xsl:apply-templates select="current-group()[not(self::p/img)] except ."/></body>
</topic>
</xsl:when>
<xsl:otherwise>
<body><xsl:apply-templates select="$image|current-group()[not(self::p/img)]"/></body>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</topic>
</xsl:if>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="p">
<p><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="img">
<xsl:element name="image">
<xsl:if test="@src">
<xsl:attribute name="href"><xsl:value-of select="@src"/></xsl:attribute>
</xsl:if>
</xsl:element>
</xsl:template>
</xsl:stylesheet>