XSLT - 需要帮助标记文本

时间:2014-08-26 15:28:38

标签: xslt

在以下示例中,我需要在bulletlist结束标记和para end标记之间的文本周围插入“indent”标记。

<para>Blah, blah, blah-blah, blah, blah, blah, blah<emphasis type="q">BL</emphasis>. </para>
<para>TheBlah, blah, blah-blah, blah, blah, blah, blah is created using the following schema:<bulletlist>
<li>First two letters. </li>
<li>Next four digits.</li>
<li>Next three digits.</li>
<li>Final six digits.</li>
</bulletlist>Here is the text I need to capture and place in an "indet" tag.</para>

我将如何在XSLT中处理此问题?我是这门语言的新手,我有点沮丧。

我还需要标记在numlist结束标记,alphalist结束标记,注释结束标记和带有“缩进标记”的段落中的表结束标记之后的文本。示例如下:

<para>Blah, blah, blah-blah, blah, blah, blah, blah<emphasis type="q">BL</emphasis>. </para>
<para>TheBlah, blah, blah-blah, blah, blah, blah, blah is created using the following schema:<numlist>
<li>First two letters. </li>
<li>Next four digits.</li>
<li>Next three digits.</li>
<li>Final six digits.</li>
</numlist>Here is the text I need to capture and place in an "indet" tag.</para>

<para>Blah, blah, blah-blah, blah, blah, blah, blah<emphasis type="q">BL</emphasis>. </para>
<para>The Blah, blah, blah-blah, blah, blah, blah, blah is created using the following schema:<numlist>
<li>First two letters. </li>
<li>Next four digits.</li>
<li>Next three digits.</li>
<li>Final six digits.</li>
</numlist>Here is the text I need to capture and place in an "indent" tag.</para>

<para>Blah, blah, blah-blah, blah, blah, blah, blah<emphasis type="q">BL</emphasis>. </para>
<para>The Blah, blah, blah-blah, blah, blah, blah, blah is created using the following schema:<note>
Blah, blah, blah, blah . . . 
</note>Here is the text I need to capture and place in an "indent" tag.</para>

<para>Blah, blah, blah-blah, blah, blah, blah, blah<emphasis type="q">BL</emphasis>. </para>
<para>The Blah, blah, blah-blah, blah, blah, blah, blah is created using the following schema:<table><tgroup cols="3" colsep="1" rowsep="3">
<colspec colname="col1" colwidth="25*"/>
<colspec colname="col2" colwidth="36*"/>
<colspec colname="col3" colwidth="39*"/>
<thead>
<row>
<entry align="center" valign="top"><emphasis type="b">Sub-status Code </emphasis></entry>
<entry align="center" valign="top"><emphasis type="b"> Sub-status Code Name</emphasis></entry>
<entry align="center" valign="top"><emphasis type="b">Description</emphasis></entry>
</row>
</thead>
<tbody>
<row>
<entry align="center" colsep="1" rowsep="1">AC</entry>
<entry colsep="1" rowsep="1">Auto Closure</entry>
<entry colsep="1" rowsep="1">Automatic closed></entry>
</row>
</tbody>
</tgroup>
</table>Here is the text I need to capture and place in an "indet" tag.</para>

有人可以提出建议吗?

我已经尝试了好几天没有成功。以下是我最近的尝试:

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

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

<xsl:template match="para-wrap/text()[following-sibling::bulletlist | alphalist | numlist | note | table]">
<indent> 
<xsl:value-of select="."/> 
</indent> 
</xsl:template>  
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

您的意思是简单地将文本节点放在indent元素中,如果它是para的子元素,并且它是bulletlist的直接跟随兄弟,{{1} }等等?然后,下面的样式表产生正确的输出。

由于你提到了元素的“结束标记”,让我为你清楚:XSLT处理器看到XML文档的方式与你看到它的方式不同。更确切地说,处理器以(称为XDM)的形式看到它的解析版本。在这棵树中,根本没有标签,既没有开始标签也没有结束标签。所有项目都只是表示为节点

因此,要求考虑元素结束标记的XSLT样式表是误导性的。您的问题必须解释为:查找紧跟note元素作为其兄弟的文本节点。

<强>样式表

bulletlist

应用于以下输入:

XML输入

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

    <xsl:strip-space elements="*"/>

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

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

        <xsl:choose>
            <xsl:when test="preceding-sibling::*[1][name() = 'bulletlist' or name() = 'alphalist' or name() = 'numlist' or name() = 'table' or name() = 'note']">
                <indent>
                    <xsl:value-of select="."/>
                </indent>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy/>
            </xsl:otherwise>
        </xsl:choose>

    </xsl:template>
</xsl:transform>

XML输出

<?xml version="1.0" encoding="UTF-8"?>
<root>
<para>Blah, blah, blah-blah, blah, blah, blah, blah<emphasis type="q">BL</emphasis>. </para>
<para>TheBlah, blah, blah-blah, blah, blah, blah, blah is created using the following schema:<bulletlist>
<li>First two letters. </li>
<li>Next four digits.</li>
<li>Next three digits.</li>
<li>Final six digits.</li>
</bulletlist>Here is the text I need to capture and place in an "indet" tag.</para>
</root>