XSLT自定义函数返回节点集或XML片段(非简单数据类型)

时间:2011-12-30 23:18:28

标签: java xslt

我正在尝试开发一个可以返回节点集或XML片段的XSLT自定义函数,让我们说:

输入文件:

<root>
<!--
 author: blablabla
 usage: more blablabla
 labelC: [in=2] <b>formatted</b> blablabla
-->
<tag1 name="first">
    <tag2>content a</tag2>
    <tag2>content b</tag2>
    <tag3 attrib="val">content c</tag3>
</tag1>

<!--
 author: blebleble
 usage: more blebleble
 labelC: blebleble
-->
<tag1 name="second">
    <tag2>content x</tag2>
    <tag2>content y</tag2>
    <tag3 attrib="val">content z</tag3>
</tag1>
</root>

这样的XSLT模板如:

    <xsl:template match="//tag1/preceding::comment()[1]" xmlns:d="java:com.dummy.func">
    <section>
     <para>
      <xsl:value-of select="d:genDoc(.)"/>
     </para>
    </section>
    </xsl:template>

会产生:

    <section>
     <para>
      <author>blablabla</author>
      <usage>more blablabla</usage>
      <labelC in="2"><b>formatted</b> blablabla</labelC>
     </para>
    </section>

在第一次出现tag1时匹配 和

    <section>
     <para>
      <author>blebleble</author>
      <usage>more blebleble</usage>
      <labelC>blebleble</labelC>
     </para>
    </section>

在第二次出现时匹配。

基本上我想用这个自定义函数实现的是解析注释中存在的一些元数据并用它来生成XML。

我在网上找到了一些例子,一个在: http://cafeconleche.org/books/xmljava/chapters/ch17s03.html

根据示例,我的函数应该返回以下

之一
org.w3c.dom.traversal.NodeIterator,
org.apache.xml.dtm.DTM,
org.apache.xml.dtm.DTMAxisIterator,
org.apache.xml.dtm.DTMIterator,
org.w3c.dom.Node and its subtypes (Element, Attr, etc),
org.w3c.dom.DocumentFragment

我能够实现一个将XML作为简单类型String返回的函数。 然而,这会带来其他几个问题:主要是标记字符在插入原始XML时会被转义。

有没有人举例说明如何实现这样的功能? 我最感兴趣的是如何将正确的XML节点集返回给调用模板。

1 个答案:

答案 0 :(得分:1)

以下可能会让您沿着您想要前往的道路前行。请注意,这需要XSLT 2.0版本(在XSLT 1.0中,在为tokenize提供替换函数时也是可能的)。另请注意,这假定具体的评论内容结构 说明:注释首先被分成行(分隔符& #xD;,这是一个换行符),然后在tag + value(分隔符“:”,分成作者,用法,labelC,这里的顺序并不重要) ,然后在labelC(delimiter“]”的属性和值中,将属性识别为以“[”开头) 请注意,使用normalize-space()进行了大量的空格擦除。

编辑:带功能的xslt版本见底部

XSLT

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

    <xsl:template match="/">
        <output>
            <xsl:apply-templates/>
        </output>
    </xsl:template>

    <xsl:template match="tag1/*">
    </xsl:template>

    <xsl:template match="comment()">
        <section>
            <para>
                <xsl:for-each select="tokenize(., '&#xD;')[string-length() != 0]">
                    <xsl:variable name="splitup" select="tokenize(normalize-space(current()), ':')"/>
                    <xsl:choose>
                        <xsl:when test="$splitup[1]='author'">
                            <author><xsl:value-of select="normalize-space($splitup[2])"/></author>
                        </xsl:when>
                        <xsl:when test="$splitup[1]='usage'">
                            <usage><xsl:value-of select="normalize-space($splitup[2])"/></usage>
                        </xsl:when>
                        <xsl:when test="$splitup[1]='labelC'">
                            <labelC>
                                <xsl:for-each select="tokenize($splitup[2], '] ')[string-length() != 0]">
                                    <xsl:variable name="labelCpart" select="normalize-space(current())"/>
                                    <xsl:choose>
                                        <xsl:when test="substring($labelCpart, 1,1) = '['">
                                            <xsl:variable name="attr" select="tokenize(substring($labelCpart, 2), '=')"/>
                                            <xsl:attribute name="{$attr[1]}"><xsl:value-of select="$attr[2]"/></xsl:attribute>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:value-of select="$labelCpart"/>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </xsl:for-each>
                            </labelC>
                        </xsl:when>
                    </xsl:choose>
                </xsl:for-each>
            </para>
        </section>
    </xsl:template>

</xsl:stylesheet>

应用于以下XML

<?xml version="1.0" encoding="UTF-8"?>
<root>
<!--
 author: blablabla
 usage: more blablabla
 labelC: [in=2] <b>formatted</b> blablabla
-->
<tag1 name="first">
    <tag2>content a</tag2>
    <tag2>content b</tag2>
    <tag3 attrib="val">content c</tag3>
</tag1>

<!--
 author: blebleble
 usage: more blebleble
 labelC: blebleble
-->
<tag1 name="second">
    <tag2>content x</tag2>
    <tag2>content y</tag2>
    <tag3 attrib="val">content z</tag3>
</tag1>
</root>

给出以下输出

<?xml version="1.0" encoding="UTF-8"?>
<output>
    <section>
        <para>
            <author>blablabla</author>
            <usage>more blablabla</usage>
            <labelC in="2">&lt;b&gt;formatted&lt;/b&gt; blablabla</labelC>
        </para>
    </section>
    <section>
        <para>
            <author>blebleble</author>
            <usage>more blebleble</usage>
            <labelC>blebleble</labelC>
        </para>
    </section>
</output>

已编辑带函数调用的xslt(提供相同的输出)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:d="java:com.dummy.func"
exclude-result-prefixes="d">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/">
        <output>
            <xsl:apply-templates/>
        </output>
    </xsl:template>

    <xsl:template match="tag1/*">
    </xsl:template>

    <xsl:function name="d:section">
        <xsl:param name="comm"/>
        <section>
            <para>
                <xsl:for-each select="tokenize($comm, '&#xD;')[string-length() != 0]">
                    <xsl:variable name="splitup" select="tokenize(normalize-space(current()), ':')"/>
                    <xsl:choose>
                        <xsl:when test="$splitup[1]='author'">
                            <author><xsl:value-of select="normalize-space($splitup[2])"/></author>
                        </xsl:when>
                        <xsl:when test="$splitup[1]='usage'">
                            <usage><xsl:value-of select="normalize-space($splitup[2])"/></usage>
                        </xsl:when>
                        <xsl:when test="$splitup[1]='labelC'">
                            <labelC>
                                <xsl:for-each select="tokenize($splitup[2], '] ')[string-length() != 0]">
                                    <xsl:variable name="labelCpart" select="normalize-space(current())"/>
                                    <xsl:choose>
                                        <xsl:when test="substring($labelCpart, 1,1) = '['">
                                            <xsl:variable name="attr" select="tokenize(substring($labelCpart, 2), '=')"/>
                                            <xsl:attribute name="{$attr[1]}"><xsl:value-of select="$attr[2]"/></xsl:attribute>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:value-of select="$labelCpart"/>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </xsl:for-each>
                            </labelC>
                        </xsl:when>
                    </xsl:choose>
                </xsl:for-each>
            </para>
        </section>
    </xsl:function>

    <xsl:template match="comment()">
        <xsl:copy-of select="d:section(.)"/>
    </xsl:template>

</xsl:stylesheet>