加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 02:30:21 所属栏目:Java 来源:网络整理
导读:我正在尝试开发一个可以返回节点集或 XML片段的XSLT自定义函数,让我们说: 输入文件: root!-- author: blablabla usage: more blablabla labelC: [in=2] bformatted/b blablabla--tag1 name="first" tag2content a/tag2 tag2content b/tag2 tag3 attrib="va
我正在尝试开发一个可以返回节点集或 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节点集返回给调用模板.

解决方法

以下可能会让您沿着您想要前往的道路前行.请注意,这需要XSLT 2.0版本(在XSLT 1.0中,当为tokenize提供替换函数时也是如此).另请注意,这假定了特定的注释内容结构.
说明:注释首先被分成行(分隔符& #xD;这是换行符),然后在标记值(分隔符“:”,分成作者,用法,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>

用函数调用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,'=')"/>
                                            <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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读