XSLT - 如果supertag包含特定术语,如何忽略XML中的子标记

时间:2012-09-21 16:06:09

标签: xslt

我正在尝试更改我的XSLT,以便当我的XML中的超级标记包含特定单词时,不会选择子标记。

在此示例中,我不希望超级标记<para>包含单词“Galaxy”时显示子标记<formalpara>

提前致谢。

我的XML

<formalpara>
Galaxy
<para>
<bridgehead>Galaxy Zoo</bridgehead>
    <sliceXML>Galaxy</sliceXML>
    The human eye is far better at identifying characteristics of galaxies 
    than any computer. So Galaxy Zoo has called for everyday citizens to 
    help in a massive identification project. Well over a hundred thousand 
    people have helped identify newly discovered galaxies. Now you can, too.
</para>
</formalpara>

我的XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sparql-results="http://www.w3.org/2005/sparql-results#" version="1.0">

<xsl:template match="/">
    <xsl:call-template name="results"/>
    <xsl:message>FROM simpleHMHTransform XSLT8</xsl:message>
</xsl:template>

<xsl:template name="results">
    <xsl:for-each select="//formalpara">
        <xsl:call-template name="formalpara"/>
    </xsl:for-each>
    <xsl:for-each select="//para">
        <xsl:call-template name="para"/>
    </xsl:for-each>
</xsl:template>

<xsl:template name="formalpara">
<div id="formalpara">
    <xsl:copy-of select="text()"/>
</div>
</xsl:template>

<xsl:template name="para">
<div id="para">
    <xsl:copy-of select="text()"/>
</div>
</xsl:template>

我当前的输出

<?xml version="1.0" encoding="UTF-8"?><div xmlns:sparql-results="http://www.w3.org/2005/sparql-results#" id="formalpara">
Galaxy

</div><div xmlns:sparql-results="http://www.w3.org/2005/sparql-results#" id="para">


    The human eye is far better at identifying characteristics of galaxies 
    than any computer. So Galaxy Zoo has called for everyday citizens to 
    help in a massive identification project. Well over a hundred thousand 
    people have helped identify newly discovered galaxies. Now you can, too.
</div>

我想要的输出

<?xml version="1.0" encoding="UTF-8"?><div xmlns:sparql-results="http://www.w3.org/2005/sparql-results#" id="formalpara">
 Galaxy
</div>

1 个答案:

答案 0 :(得分:2)

你应该真正做xsl:apply-templates而不是调用命名模板。然后,您可以添加此模板:

<xsl:template match="formalpara[contains(text(),'Galaxy')]/para"/>

我稍后可以举一个完整的例子。


完整示例:

XML输入

<formalpara>
    Galaxy
    <para>
        <bridgehead>Galaxy Zoo</bridgehead>
        <sliceXML>Galaxy</sliceXML>
        The human eye is far better at identifying characteristics of galaxies 
        than any computer. So Galaxy Zoo has called for everyday citizens to 
        help in a massive identification project. Well over a hundred thousand 
        people have helped identify newly discovered galaxies. Now you can, too.
    </para>
</formalpara>

XSLT 1.0

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

    <xsl:template match="formalpara">
        <div id="formalpara">
            <xsl:apply-templates/>
        </div>
    </xsl:template>

    <xsl:template match="formalpara[contains(text(),'Galaxy')]/para"/>

    <xsl:template match="para">
        <div id="para">
            <xsl:apply-templates/>
        </div>
    </xsl:template>

</xsl:stylesheet>

XML输出

<div xmlns:sparql-results="http://www.w3.org/2005/sparql-results#" id="formalpara">
    Galaxy
    </div>

注意:我还将contains()更改为contains(text(),'Galaxy'),因此它只会查看formalpara的直接子项文本。