XSLT 无法删除属性值前后的换行符

XSLT unable to remove line break before and after attribute value

我正在尝试将 XSLT 3.0 和 Saxon 9.9 HE 的 XML 模式转换为 JSON。转型正在奏效。但是,当我输出一个属性的值时,它在名称前后都有一个换行符。 XSD 片段如下:

    <group name="imapAttachmentDownloaderGroup">
    <annotation>
        <documentation>
            Configuration block definitions for IMAP attachment downloader delegate. It contains all the configuration
            block definitions that are required to configure the delegate successfully.
        </documentation>
    </annotation>
    <sequence>
        <element name="IMAPConnectorConfig">
            <complexType>
                <attribute name="imapHost" use="required" type="string">
                    <annotation>
                        <documentation>
                            This configuration block deals with the IMAP server name or IP address to where the 
                            connection is to be made to download the attachments.
                            Example: "outlook.office365.com" or "40.100.137.66"
                        </documentation>
                    </annotation>
                </attribute>
                <attribute name="imapPort" use="required" type="int">
                    <annotation>
                        <documentation>
                            The port to use to connect to the server, defaults to 993.
                            Example: "993"
                        </documentation>
                    </annotation>
                </attribute>
....

SXL如下:

<?xml version="1.0" encoding="UTF-8"?>
<stylesheet version="3.0" 
            xmlns="http://www.w3.org/1999/XSL/Transform"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema"
            xmlns:tns="http://www.proviseconsulting.com/ProcessConfig"
            xmlns:saxon="http://saxon.sf.net/"
            xsi:schemaLocation="http://www.w3.org/1999/XSL/Transform 
                                https://www.w3.org/2007/schema-for-xslt20.xsd">

<mode on-no-match="shallow-skip"/>
<output method="text" encoding="utf-8" indent="no" media-type="application/json" normalization-form="true"/>
<strip-space elements="*" />
<!-- <template match="text()|@*"/> -->
<param name="delegateGroupName" required="yes"/>

<template match="/child::xsi:schema/child::xsi:group[attribute::name=$delegateGroupName]">
    <variable name="xx" select="attribute::name"/>
    {'groupName':'<value-of select="normalize-space($xx)"/>'
                  <for-each select="child::xsi:sequence/child::xsi:element">
                    ,'<value-of select="attribute::name"/>':{
                        <for-each select="child::xsi:complexType/child::xsi:attribute">
                            <choose>
                                <when test="position()=last()">'name':'<value-of select="attribute::name"/>'</when>
                                <otherwise>'name':'<value-of select="attribute::name"/>',</otherwise>
                            </choose>
                        </for-each>
                        <for-each select="child::xsi:complexType/child::xsi:attributeGroup">
                            <value-of select="attribute::ref"/>
                        </for-each>
                                                            }
                  </for-each>
        }

    <!-- <apply-templates select="attribute::name"/> -->
</template>

输出为:

    {'groupName':'
imapAttachmentDownloaderGroup
'
,    '
IMAPConnectorConfig
':{
'name':'
imapHost
',
'name':'
imapPort
',
'name':'
sslEnabled
',
'name':'
startTLSEnabled
',
'name':'
imapUser
',
'name':'
imapPassword
',
'name':'
credentialId

我已经尝试了所有我能在帖子中找到的机制,将其分配给变量,normalize-space,normalize-space with string() with data()。似乎没有任何效果。任何帮助将不胜感激。

=== 包括完整示例 ===

XSL(删除了所有注释和其他元素):

<?xml version="1.0" encoding="UTF-8"?>
<stylesheet version="3.0" 
            xmlns="http://www.w3.org/1999/XSL/Transform"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema"
            xmlns:tns="http://www.proviseconsulting.com/ProcessConfig"
            xmlns:saxon="http://saxon.sf.net/"
            xsi:schemaLocation="http://www.w3.org/1999/XSL/Transform 
                                https://www.w3.org/2007/schema-for-xslt20.xsd">

<mode on-no-match="shallow-skip"/>
<output method="text" encoding="utf-8" indent="no" media-type="application/json" normalization-form="true"/>
<strip-space elements="*" />

<param name="delegateGroupName" required="yes"/>

<template match="/child::xsi:schema/child::xsi:group[attribute::name=$delegateGroupName]">
    <text>{"groupName":"</text><value-of select="attribute::name/normalize-space()"/><text>"}</text>
</template>

=== 输出 ===

{"groupName":"
imapAttachmentDownloaderGroup
"}

输入XSD保持不变。 Java代码如下:

        String xsdFilePath="file:///D:/workspaces/mtplatform/PlatformManual_V1/PlatformManual/ProcessConfiguration/ProcessConfiguration.xsd";
    String xslFilePath="file:///D:/workspaces/mtplatform/PlatformManual_V1/PlatformManual/ProcessConfiguration/ProcessConfiguration.xsl";

    Processor processor=new Processor(false);
    XdmNode node=processor.newDocumentBuilder().build(new File(new URI(xsdFilePath)));
    XsltCompiler xsltCompiler=processor.newXsltCompiler();
    //xsltCompiler.setParameter(new QName("delegateGroupName"), XdmValue.makeValue("imapAttachmentDownloaderGroup"));

    StreamSource xsdSource=new StreamSource(new FileInputStream(new File(new URI(xsdFilePath))));
    StreamSource xslSource=new StreamSource(new FileInputStream(new File(new URI(xslFilePath))));

    XsltExecutable compiledXSL=xsltCompiler.compile(xslSource);
    Xslt30Transformer xslTransformer=compiledXSL.load30();

    HashMap<QName, XdmValue> parameterMap=new HashMap<>();
    parameterMap.put(new QName("delegateGroupName"), XdmValue.makeValue("imapAttachmentDownloaderGroup"));
    xslTransformer.setStylesheetParameters(parameterMap);
    XdmValue output=xslTransformer.applyTemplates(node);

    System.out.println(output.toString());

一般情况下,要控制文本输出,可以在表单

中使用xsl:text元素https://www.w3.org/TR/xslt-30/#xsl-text
 <for-each select="child::xsi:sequence/child::xsi:element">
                <text>,'</text>
                <value-of select="attribute::name"/>
                <text>':{</text>

此外,如果您使用 Xslt30Transformer 并想要文本形式的序列化结果,例如 JSON,其中格式由 XSLT 中的 xsl:output 定义,那么您应该使用 applyTemplates 的重载,允许您使用 Serializer 作为目标,即 http://saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/Xslt30Transformer.html#applyTemplates-net.sf.saxon.s9api.XdmValue-net.sf.saxon.s9api.Destination- with a Serializer created by the Xslt30Transformer (http://saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/Xslt30Transformer.html#newSerializer-java.io.OutputStream-),因此在使用 System.out 测试结果的简单情况下,您会使用

Serializer serializer =  xslTransformer.newSerializer(System.out);
xslTransformer.applyTemplates(node, serializer);

在您的 Java 案例中,您使用重载创建了一个 XdmValue,请注意它的文档 http://saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/Xslt30Transformer.html#applyTemplates-net.sf.saxon.s9api.XdmValue- 说它 returns

the raw result of applying templates to the supplied selection value, without wrapping in a document node or serializing the result

正如您根据评论发现的那样,如果您有一个 XdmValue 并希望以受控方式对其进行序列化,您最好使用 [=16] 的 serializeXdmValue 方法=] 根据需要设置而不是简单地在 XdmValue.

上调用 toString