首页 文章

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

提问于
浏览
4

我正在尝试开发一个可以返回节点集或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>

在第二次出现时匹配 .

Basically what I want to achieve with this custom function is to parse some meta-data present in the comments and use it to generate 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 回答

  • 1

    以下可能会让您沿着您想要前往的道路前行 . 请注意,这需要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>
    

    当应用于以下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>
    

    EDITED 带函数调用的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>
    

相关问题