XQuery - 是否可以生成非XML文档?

时间:2012-07-09 11:37:08

标签: xml xquery

我想使用XQuery从我的XML文档生成Java源代码,例如:

<configuration package="my.package.name">

    <property>
        <name>First</name>
        <value>0</value>
        <description>First description</description>
    </property>

    <property>
        <name>Second</name>
        <value>2</value>
        <description>Second description</description>
    </property>
...

应该生成:

package my.package.name;

class MyClass {
    // First description
    private String first;

    // Second description
    private String second;
}

我试图这样开始:

xquery version "1.0";

"package "+$doc/@package
"class "+$doc/@classname
{
for $property in $doc//property
    return {
        "private String "+$property/name::text()
    }
}

语法明显不正确,我想知道它是否可行。谢谢!

2 个答案:

答案 0 :(得分:1)

详细信息取决于您的查询处理器。应该可以将查询结果作为字符串返回,也可以像在XSL中一样使用文本序列化方法;但是你调用它的方式因查询处理器而异。 (两种方法的区别在于文本序列化方法会自动将查询结果中的任何内容转换为字符串,然后连接字符串)。

以下是如何以返回单个字符串的查询形式执行此操作:

declare variable $nl "= '&#10;';
concat(
  "package ", $doc/@package, $nl,
  "class ", $doc/@classname, $nl,
  string-join(
    (for $property in $doc//property
     return {
        concat("private String ", $property/name)
     }), $nl)
)

答案 1 :(得分:1)

的XQuery:

declare function xf:XML2Java($configuration as element(configuration))
    as xs:string {
    let $package := string($configuration/@package)
    let $newline := "&#13;"
       for $property in $configuration/property
       let $variable := data($property/name)
       let $javadoc := data($property/description)
       return
        fn:concat($package,$newline,"class MyClass{",$newline,"//",$javadoc,$newline,"private String ",$variable,";",$newline," }")     
};

输入:

<configuration package="com.test">
    <property>
        <name>Test</name>
        <value>somevalue</value>
        <description>This is test variable</description>
    </property>
</configuration>

输出:

com.test
class MyClass{
//This is test variable
private String Test;
 }

在WebLogic Workshop XQuery designer中做过这个。