首页 文章

无法使用name在node-set()结果中选择元素,但*找到它

提问于
浏览
-2

我有一个XSLT 1.0 *样式表,它执行一些预处理并创建一个结果片段,其中包含一个元素列表 <x> ,每个元素都有两个子节点 - 让我们调用 <a><b> .

所以生成的列表如下所示:

<x><a>A-content</a><b>B-content</b></x>
<x><a>A-content</a><b>B-content</b></x>
...
<x><a>A-content</a><b>B-content</b></x>

然后我使用node-set()将其转换为节点集,并使用apply-templates将所有 <x> 元素转换为输出表示 .

到现在为止还挺好 .

但是我必须在输出模板上使用 match="*" 规则,虽然我可以使用 "*[1]""*[2]" 获取子元素,但我找不到它们使用 "a""b" - 我只是得到一个空结果 .

位置语法作为一种解决方法,但它相当脆弱,我想将其更改为处理元素名称 . 而且,它不是很可读 .

我确实怀疑它可能是命名空间问题( <x><a><b> 未在输入或输出文档的原始模式中定义),但据我所知,当使用"*"选择元素时,没有命名空间装饰 .

万一它很重要,我在cygwin下使用xsltproc(libxml 20902,libxslt 10128和libexslt 817) .

关于我可能做错的任何想法,或者有关调试的提示?

(* - 我必须使用XSLT 1.0,因为它设计为在Web浏览器中运行 . )


编辑:根据要求添加示例

输入test.xml:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="test.xsl" type="text/xsl" ?>

<books>
    <book>
        <title>Diaspora</title>
        <author>Greg Egan</author>
    </book>

    <book>
        <title>2001</title>
        <author>Arthur C Clarke</author>
    </book>

    <book>
        <title>Eon</title>
        <author>Greg Bear</author>
    </book>
</books>

转换test.xslt:

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.w3.org/1999/xhtml"
                                xmlns:exslt="http://exslt.org/common"
                                xmlns:xalan="http://xml.apache.org/xalan"
                                xmlns:msxslt="urn:schemas-microsoft-com:xslt"
                                exclude-result-prefixes="xsl msxslt exslt xalan">
<!--    extension-element-prefixes="exslt"> -->

    <xsl:template match="books">
        <!-- Generate list -->
        <xsl:variable name="list">
            <xsl:apply-templates select="book" mode="phase1"/>
        </xsl:variable>

        <html>
            <head>
                <title>Books</title>
            </head>
            <body>
                <xsl:choose>
                    <xsl:when test="function-available('msxslt:node-set')">
                        <xsl:apply-templates select="msxslt:node-set($list)" mode="process-list"/>
                    </xsl:when>
                    <xsl:when test="function-available('exslt:node-set')">
                        <xsl:apply-templates select="exslt:node-set($list)" mode="process-list"/>
                    </xsl:when>
                    <xsl:when test="function-available('xalan:nodeset')">
                        <xsl:apply-templates select="xalan:nodeset($list)"  mode="process-list"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="$list" mode="process-list"/>
                    </xsl:otherwise>
                </xsl:choose>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="book" mode="phase1">
        <!-- Actual transformation is more involved -->
        <xsl:element name="x">
            <xsl:element name="a">
                <b>
                    <xsl:value-of select="author/text()"/>
                </b>
            </xsl:element>
            <xsl:element name="b">
                <i>
                    <xsl:value-of select="title/text()"/>
                </i>
            </xsl:element>
        </xsl:element>
    </xsl:template>

    <xsl:template match="*" mode="process-list">
        <p>
            [<xsl:value-of select="*[1]"/>]
            [<xsl:value-of select="*[2]"/>]
            [<xsl:value-of select="a"/>]
            [<xsl:value-of select="b"/>]
        </p>
    </xsl:template>

</xsl:stylesheet>

输出(msxslt和xsltproc的输出相同):

<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head><title>Books</title></head>
    <body>
        <p>
            [Greg Egan]
            [Diaspora]
            []
            []
        </p><p>
            [Arthur C Clarke]
            [2001]
            []
            []
        </p><p>
            [Greg Bear]
            [Eon]
            []
            []
        </p>
    </body>
</html>

2 回答

  • 0

    我继续搜索并找到了解决方案 . 我们怀疑,这是一个名称空间问题 - 这是描述它的previous post .

    尽管我尝试将新元素放在新的命名空间中,但它们仍然进入默认命名空间,我声明为:

    xmlns="http://www.w3.org/1999/xhtml"
    

    但是,这不会被视为XPATH表达式中的默认值,因此未找到它 . (次要问题 - 为什么不呢?)

    解决方案是使用命名空间前缀重复声明默认命名空间:

    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    

    并在xpath中显式使用该前缀:

    [<xsl:copy-of select="xhtml:a"/>]
    [<xsl:copy-of select="xhtml:b"/>]
    

    然后一切都匹配,我从命名和基于位置的XPATH表达式得到相同的输出 .

    感谢大家充当声音板 - 我希望以后可以帮助别人 .

  • -1

    我只能假设,您正在使用exslt扩展来创建节点集,我想您正在尝试设置多次传递转换:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:exsl="http://exslt.org/common"
                    extension-element-prefixes="exsl">
    
    <xsl:template match="/">
      <!-- create your first pass here -->
      <xsl:variable name="first-pass">
          <xsl:apply-templates mode="first-pass"/>
      </xsl:variable>
      <xsl:apply-templates select="exsl:node-set($first-pass)" mode="second-pass"/>
    </xsl:template>
    
    <!-- implementation of first-pass
        ....
     -->
    
    <!-- second-pass: find a and b elements in x -->
    <xsl:template match="x/a" mode="second-pass">
        <!-- your turn -->
    </xsl:template>
    
    <xsl:template match="x/b" mode="second-pass">
        <!-- your turn -->
    </xsl:template>
    
    <xsl:template match="@*|node()" mode="second-pass">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" mode="second-pass"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="@*|node()" mode="first-pass">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" mode="first-pass"/>
        </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

相关问题