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

Dan*_*ele 5 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>
Run Code Online (Sandbox Code Playgroud)

这样一个 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>
Run Code Online (Sandbox Code Playgroud)

会产生:

    <section>
     <para>
      <author>blablabla</author>
      <usage>more blablabla</usage>
      <labelC in="2"><b>formatted</b> blablabla</labelC>
     </para>
    </section>
Run Code Online (Sandbox Code Playgroud)

当第一次出现 tag1 和

    <section>
     <para>
      <author>blebleble</author>
      <usage>more blebleble</usage>
      <labelC>blebleble</labelC>
     </para>
    </section>
Run Code Online (Sandbox Code Playgroud)

第二次匹配时。

基本上我想用这个自定义函数实现的是解析注释中存在的一些元数据并使用它来生成 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
Run Code Online (Sandbox Code Playgroud)

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

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

Mae*_*o13 2

下面的内容可能会让你在你想要走的路上走得更远。请注意,这需要 XSLT 2.0 版本(在 XSLT 1.0 中,当提供 的替换函数时,这也是可能的tokenize)。另请注意,这假定了特定的评论内容结构。
说明:注释首先被分成行(分隔符& #xD;是换行符),然后是标签+值(分隔符“:”,分成作者,用法,labelC,这里顺序不重要),然后是属性和labelC 的值(分隔符“]”,将属性识别为以“[”开头)。
请注意,大量空白擦除是使用 完成的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>
Run Code Online (Sandbox Code Playgroud)

当应用于以下 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>
Run Code Online (Sandbox Code Playgroud)

给出以下输出

<?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>
Run Code Online (Sandbox Code Playgroud)

编辑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>
Run Code Online (Sandbox Code Playgroud)