1

更新这篇文章,以显示我目前在哪里。因此,在我的回答帖子中进行了一些澄清之后,我将更新这篇帖子,并更新当前的进展,但目前的进展并不那么闪亮。XSLT 样式表当前如下所示:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

    <xsl:template match="value" mode="values" />

    <xsl:template match="value[@name = 'field_name' or @name = 'field_type' or @name = 'field_dim']" mode="values">
        <xsl:element name="{@name}">
            <xsl:value-of select="text()"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="record[ancestor::record[@name='sig_in' or @name='sig_out'] and value[@name = 'field_name']]">
        <xsl:param name="path" />

        <xsl:variable name ="newpath">
            <xsl:value-of select="concat($path,'/',  value[@name = 'field_name' ]/text())" />
        </xsl:variable>

        <xsl:copy>
            <xsl:attribute name="path">
                <xsl:value-of select="$newpath" />
            </xsl:attribute>

            <xsl:apply-templates mode="values" select="value" />

            <xsl:apply-templates>
                <xsl:with-param name="path" select="$newpath" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

来自服务 xml 签名的输入文档:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Values version="2.0">
    <value name="svc_type">flow</value>
    <value name="svc_subtype">default</value>
    <value name="svc_sigtype">java 3.5</value>
    <record javaclass="com.wm.util.Values" name="svc_sig">
        <record javaclass="com.wm.util.Values" name="sig_in">
            <value name="node_type">record</value>
            <value name="is_public">false</value>
            <value name="field_type">record</value>
            <value name="field_dim">0</value>
            <value name="nillable">true</value>
            <value name="form_qualified">false</value>
            <value name="is_global">false</value>
            <array depth="1" name="rec_fields" type="record">
                <record javaclass="com.wm.util.Values">
                    <value name="node_type">record</value>
                    <value name="node_comment"/>
                    <record javaclass="com.wm.util.Values" name="node_hints">
                        <value name="field_largerEditor">false</value>
                        <value name="field_password">false</value>
                    </record>
                    <value name="is_public">false</value>
                    <value name="field_name">docTest</value>
                    <value name="field_type">record</value>
                    <value name="field_dim">0</value>
                    <value name="nillable">true</value>
                    <value name="form_qualified">false</value>
                    <value name="is_global">false</value>
                    <array depth="1" name="rec_fields" type="record">
                        <record javaclass="com.wm.util.Values">
                            <value name="node_type">record</value>
                            <value name="node_comment"/>
                            <record javaclass="com.wm.util.Values" name="node_hints">
                                <null name="field_usereditable"/>
                                <value name="field_largerEditor">false</value>
                                <value name="field_password">false</value>
                            </record>
                            <value name="is_public">false</value>
                            <value name="field_name">doc.name</value>
                            <value name="field_type">string</value>
                            <value name="field_dim">0</value>
                            <value name="nillable">true</value>
                            <value name="form_qualified">false</value>
                            <value name="is_global">false</value>
                        </record>
                    </array>
                    <value name="rec_closed">true</value>
                    <value name="modifiable">true</value>
                </record>
                <record javaclass="com.wm.util.Values">
                    <value name="node_type">record</value>
                    <value name="node_comment"/>
                    <record javaclass="com.wm.util.Values" name="node_hints">
                        <value name="field_largerEditor">false</value>
                        <value name="field_password">false</value>
                    </record>
                    <value name="is_public">false</value>
                    <value name="field_name">docListTest</value>
                    <value name="field_type">record</value>
                    <value name="field_dim">1</value>
                    <value name="nillable">true</value>
                    <value name="form_qualified">false</value>
                    <value name="is_global">false</value>
                    <value name="is_soap_array_encoding_used">false</value>
                    <array depth="1" name="rec_fields" type="record">
                        <record javaclass="com.wm.util.Values">
                            <value name="node_type">record</value>
                            <value name="node_comment"/>
                            <record javaclass="com.wm.util.Values" name="node_hints">
                                <value name="field_largerEditor">false</value>
                                <value name="field_password">false</value>
                            </record>
                            <value name="is_public">false</value>
                            <value name="field_name">d0</value>
                            <value name="field_type">record</value>
                            <value name="field_dim">0</value>
                            <value name="nillable">true</value>
                            <value name="form_qualified">false</value>
                            <value name="is_global">false</value>
                            <array depth="1" name="rec_fields" type="record">
                                <record javaclass="com.wm.util.Values">
                                    <value name="node_type">record</value>
                                    <value name="node_comment"/>
                                    <record javaclass="com.wm.util.Values" name="node_hints">
                                        <null name="field_usereditable"/>
                                        <value name="field_largerEditor">false</value>
                                        <value name="field_password">false</value>
                                    </record>
                                    <value name="is_public">false</value>
                                    <value name="field_name">d0.name</value>
                                    <value name="field_type">string</value>
                                    <value name="field_dim">0</value>
                                    <value name="nillable">true</value>
                                    <value name="form_qualified">false</value>
                                    <value name="is_global">false</value>
                                </record>
                            </array>
                            <value name="rec_closed">true</value>
                            <value name="modifiable">true</value>
                        </record>
                    </array>
                    <value name="rec_closed">true</value>
                    <value name="modifiable">true</value>
                </record>
                <record javaclass="com.wm.util.Values">
                    <value name="node_type">record</value>
                    <value name="node_comment"/>
                    <record javaclass="com.wm.util.Values" name="node_hints">
                        <value name="field_largerEditor">false</value>
                        <value name="field_password">false</value>
                    </record>
                    <value name="is_public">false</value>
                    <value name="field_name">packages_should_work</value>
                    <value name="field_type">recref</value>
                    <value name="field_dim">0</value>
                    <value name="nillable">true</value>
                    <value name="form_qualified">false</value>
                    <value name="is_global">false</value>
                    <value name="rec_closed">true</value>
                    <value name="modifiable">true</value>
                    <value name="rec_ref">data:packages</value>
                </record>
                <record javaclass="com.wm.util.Values">
                    <value name="node_type">record</value>
                    <value name="node_comment"/>
                    <record javaclass="com.wm.util.Values" name="node_hints">
                        <value name="field_largerEditor">false</value>
                        <value name="field_password">false</value>
                    </record>
                    <value name="is_public">false</value>
                    <value name="field_name">packages_list_should_work</value>
                    <value name="field_type">recref</value>
                    <value name="field_dim">1</value>
                    <value name="nillable">true</value>
                    <value name="form_qualified">false</value>
                    <value name="is_global">false</value>
                    <value name="is_soap_array_encoding_used">false</value>
                    <value name="rec_closed">true</value>
                    <value name="modifiable">true</value>
                    <value name="rec_ref">data:packages</value>
                </record>
            </array>
            <value name="modifiable">true</value>
        </record>
        <record javaclass="com.wm.util.Values" name="sig_out">
            <value name="node_type">record</value>
            <value name="is_public">false</value>
            <value name="field_type">record</value>
            <value name="field_dim">0</value>
            <value name="nillable">true</value>
            <value name="form_qualified">false</value>
            <value name="is_global">false</value>
            <array depth="1" name="rec_fields" type="record">
                <record javaclass="com.wm.util.Values">
                    <value name="node_type">record</value>
                    <value name="node_comment"/>
                    <record javaclass="com.wm.util.Values" name="node_hints">
                        <null name="field_usereditable"/>
                        <value name="field_largerEditor">false</value>
                        <value name="field_password">false</value>
                    </record>
                    <value name="is_public">false</value>
                    <value name="field_name">doc</value>
                    <value name="field_type">string</value>
                    <value name="field_dim">0</value>
                    <value name="nillable">true</value>
                    <value name="form_qualified">false</value>
                    <value name="is_global">false</value>
                </record>
                <record javaclass="com.wm.util.Values">
                    <value name="node_type">record</value>
                    <value name="node_comment"/>
                    <record javaclass="com.wm.util.Values" name="node_hints">
                        <value name="field_largerEditor">false</value>
                        <value name="field_password">false</value>
                    </record>
                    <value name="is_public">false</value>
                    <value name="field_name">lDoc</value>
                    <value name="field_type">string</value>
                    <value name="field_dim">1</value>
                    <value name="nillable">true</value>
                    <value name="form_qualified">false</value>
                    <value name="is_global">false</value>
                    <value name="is_soap_array_encoding_used">false</value>
                </record>
                <record javaclass="com.wm.util.Values">
                    <value name="node_type">record</value>
                    <value name="node_comment"/>
                    <record javaclass="com.wm.util.Values" name="node_hints">
                        <null name="field_usereditable"/>
                        <value name="field_largerEditor">false</value>
                        <value name="field_password">false</value>
                    </record>
                    <value name="is_public">false</value>
                    <value name="field_name">docR</value>
                    <value name="field_type">string</value>
                    <value name="field_dim">0</value>
                    <value name="nillable">true</value>
                    <value name="form_qualified">false</value>
                    <value name="is_global">false</value>
                </record>
                <record javaclass="com.wm.util.Values">
                    <value name="node_type">record</value>
                    <value name="node_comment"/>
                    <record javaclass="com.wm.util.Values" name="node_hints">
                        <value name="field_largerEditor">false</value>
                        <value name="field_password">false</value>
                    </record>
                    <value name="is_public">false</value>
                    <value name="field_name">lDocR</value>
                    <value name="field_type">string</value>
                    <value name="field_dim">1</value>
                    <value name="nillable">true</value>
                    <value name="form_qualified">false</value>
                    <value name="is_global">false</value>
                    <value name="is_soap_array_encoding_used">false</value>
                </record>
            </array>
            <value name="modifiable">true</value>
        </record>
    </record>
    <value name="stateless">no</value>
    <value name="caching">no</value>
    <value name="prefetch">no</value>
    <value name="cache_ttl">15</value>
    <value name="prefetch_level">1</value>
    <value name="template">testfolder_documentTest</value>
    <value name="template_type">html</value>
    <value name="audit_level">off</value>
    <value name="check_internal_acls">no</value>
    <value name="icontext_policy">$null</value>
    <value name="system_service">no</value>
    <value name="retry_max">0</value>
    <value name="retry_interval">0</value>
    <value name="svc_in_validator_options">none</value>
    <value name="svc_out_validator_options">none</value>
    <value name="auditoption">0</value>
    <null name="auditfields_input"/>
    <null name="auditfields_output"/>
    <record javaclass="com.wm.util.Values" name="auditsettings">
        <value name="document_data">0</value>
        <value name="startExecution">false</value>
        <value name="stopExecution">false</value>
        <value name="onError">true</value>
    </record>
    <value name="pipeline_option">1</value>
    <null name="originURI"/>
    <value name="modifiable">true</value>
    <value name="is_public">false</value>
</Values>

转换后的示例 xml(所需格式):

<?xml version="1.0" encoding="UTF-8"?>
<Values name = "svc_sig">
    <Inputs name = "sig_in">
        <record path = "sig_in/docTest">
            <field_name>docTest</field_name>
            <dield_type>record</dield_type>
            <field_dimension>value</field_dimension>
            <record path = "sig_in/docTest/doc.name">
                <field_name>doc.name</field_name>
                <field_type>string</field_type>
                <field_dimension>value</field_dimension>
            </record>
        </record>
        <record path = "sig_in/docListTest">
            <field_name>docListTest</field_name>
            <field_type>record</field_type>
            <field_dimension>array</field_dimension>
            <record path = "sig_in/docListTest/d0">
                <field_name>d0</field_name>
                <field_type>record</field_type>
                <field_dimension>value</field_dimension>
                <record path = "sig_in/docListTest/d0/d0.name">
                    <field_name>d0.name</field_name>
                    <field_type>string</field_type>
                    <field_dimension>value</field_dimension>
                </record>
            </record>
        </record>
        <record path = "sig_in/packages_should_work">
            <field_name>packages_should_work</field_name>
            <field_type>recref</field_type>
            <field_dimension>value</field_dimension>
            <record path = "sig_in/packages_should_work/data:packages" />
        </record>
        <record path = "sig_in/packages_list_should_work">
            <field_name>packages_list_should_work</field_name>
            <field_type>recref</field_type>
            <field_dimension>array</field_dimension>
            <record path = "sig_in/packages_list_should_work/data:packages" />
        </record>
    </Inputs>
    <Outputs name = "sig_out">
        <record path = "sig_out/doc">
            <field_name>doc</field_name>
            <field_type>string</field_type>
            <field_dimension>value</field_dimension>
        </record>
        <record path = "sig_out/lDoc">
            <field_name>lDoc</field_name>
            <field_type>string</field_type>
            <field_dimension>array</field_dimension>
        </record>
        <record path = "sig_out/docR">
            <field_name>docR</field_name>
            <field_type>string</field_type>
            <field_dimension>value</field_dimension>
        </record>
        <record path = "sig_out/lDocR">
            <field_name>lDocR</field_name>
            <field_type>string</field_type>
            <field_dimension>array</field_dimension>
        </record>
    </Outputs>
</Values>

此样式表当前产生的内容(与预期格式相去甚远......):目前仅尝试在记录中包含 field_name 节点,而将所有其他节点排除在外。

<?xml version="1.0" encoding="UTF-8"?>
    flow
    default
    java 3.5


            record
            false
            record
            0
            true
            false
            false

                <record path="/docTest">
   <field_name>docTest</field_name>
   <field_type>record</field_type>
   <field_dim>0</field_dim>
                    record


                        false
                        false

                    false
                    docTest
                    record
                    0
                    true
                    false
                    false

                        <record path="/doc.name">
      <field_name>doc.name</field_name>
      <field_type>string</field_type>
      <field_dim>0</field_dim>
                            record



                                false
                                false

                            false
                            doc.name
                            string
                            0
                            true
                            false
                            false
                        </record>

                    true
                    true
                </record>

<record path="/docListTest">
   <field_name>docListTest</field_name>
   <field_type>record</field_type>
   <field_dim>1</field_dim>
                    record


                        false
                        false

                    false
                    docListTest
                    record
                    1
                    true
                    false
                    false
                    false

                        <record path="/d0">
      <field_name>d0</field_name>
      <field_type>record</field_type>
      <field_dim>0</field_dim>
                            record


                                false
                                false

                            false
                            d0
                            record
                            0
                            true
                            false
                            false

                                <record path="/d0.name">
         <field_name>d0.name</field_name>
         <field_type>string</field_type>
         <field_dim>0</field_dim>
                                    record



                                        false
                                        false

                                    false
                                    d0.name
                                    string
                                    0
                                    true
                                    false
                                    false
                                </record>

                            true
                            true
                        </record>

                    true
                    true
                </record>

<record path="/packages_should_work">
   <field_name>packages_should_work</field_name>
   <field_type>recref</field_type>
   <field_dim>0</field_dim>
                    record


                        false
                        false

                    false
                    packages_should_work
                    recref
                    0
                    true
                    false
                    false
                    true
                    true
                    data:packages
                </record>

<record path="/packages_list_should_work">
   <field_name>packages_list_should_work</field_name>
   <field_type>recref</field_type>
   <field_dim>1</field_dim>
                    record


                        false
                        false

                    false
                    packages_list_should_work
                    recref
                    1
                    true
                    false
                    false
                    false
                    true
                    true
                    data:packages
                </record>

            true


            record
            false
            record
            0
            true
            false
            false

                <record path="/doc">
   <field_name>doc</field_name>
   <field_type>string</field_type>
   <field_dim>0</field_dim>
                    record



                        false
                        false

                    false
                    doc
                    string
                    0
                    true
                    false
                    false
                </record>

<record path="/lDoc">
   <field_name>lDoc</field_name>
   <field_type>string</field_type>
   <field_dim>1</field_dim>
                    record


                        false
                        false

                    false
                    lDoc
                    string
                    1
                    true
                    false
                    false
                    false
                </record>

<record path="/docR">
   <field_name>docR</field_name>
   <field_type>string</field_type>
   <field_dim>0</field_dim>
                    record



                        false
                        false

                    false
                    docR
                    string
                    0
                    true
                    false
                    false
                </record>

<record path="/lDocR">
   <field_name>lDocR</field_name>
   <field_type>string</field_type>
   <field_dim>1</field_dim>
                    record


                        false
                        false

                    false
                    lDocR
                    string
                    1
                    true
                    false
                    false
                    false
                </record>

            true


    no
    no
    no
    15
    1
    testfolder_documentTest
    html
    off
    no
    $null
    no
    0
    0
    none
    none
    0



        0
        false
        false
        true

    1

    true
    false

Transformer 设置为 Xalan。在 JAVA 中,如果我尝试运行此转换器,则会出现以下异常:

[Fatal Error] :2:3: Content is not allowed in prolog.

虽然进行实际转换的代码如下:

private static Document tme(Document o) throws Exception {
        StringWriter sw = new StringWriter();
        StreamSource s = new StreamSource(
                new StringReader("XSLT IN HERE as bare string copied from oxygen author"));
        Transformer transformer = TransformerFactory.newInstance().newTransformer(s);


        StreamResult res = new StreamResult(sw);
        transformer.transform(new DOMSource(o.getDocumentElement()), res);

        return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(sw.toString())));
    }
4

2 回答 2

1

尝试这样的事情:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>

    <xsl:template match="value"  mode="values"/>

    <xsl:template match="value[@name = 'field_name' or
                            @name = 'field_type' or
                            @name = 'field_dim']" mode="values">
        <xsl:element name="{@name}">
            <xsl:value-of select="text()"/>
        </xsl:element>

    </xsl:template>

    <xsl:template match="record[ ancestor::record[@name='sig_in' or @name='sig_out'] and 
                             value[@name = 'field_name' ]]">
        <xsl:param name="path" />
        <xsl:variable name ="newpath">
            <xsl:value-of select="concat($path,'/',  value[@name = 'field_name' ]/text())" />
        </xsl:variable>
        <xsl:copy>
            <xsl:attribute name="path">
                <xsl:value-of select="$newpath"/>
            </xsl:attribute>
            <xsl:apply-templates  mode="values" select="value"/>
            <xsl:apply-templates  >
                <xsl:with-param name="path" select="$newpath" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>



    <xsl:template match="record[@name='sig_in']" >
        <Inputs name="sig_in" >
            <xsl:apply-templates  >
                <xsl:with-param name="path" select="'sig_in'" />
            </xsl:apply-templates>
        </Inputs>
    </xsl:template>

    <xsl:template match="record[@name='sig_out']" >
        <Outputs name = "sig_out">
            <xsl:apply-templates  >
                <xsl:with-param name="path" select="'sig_out'" />
            </xsl:apply-templates>
        </Outputs>
    </xsl:template>

    <xsl:template match="Values[descendant::record[@name='svc_sig'] ]">
        <Values name="svc_sig">
            <xsl:apply-templates  select="descendant::record[@name='svc_sig']"/>
        </Values>
    </xsl:template>

    <xsl:template match="node()">
        <xsl:param name="path" />
        <xsl:apply-templates  select="node()" >
            <xsl:with-param name="path" select="$path" />
        </xsl:apply-templates>

    </xsl:template>

</xsl:stylesheet>

这将生成以下输出:

<?xml version="1.0"?>
<Values name="svc_sig">
  <Inputs name="sig_in">
    <record path="sig_in/docTest">
      <field_name>docTest</field_name>
      <field_type>record</field_type>
      <field_dim>0</field_dim>
      <record path="sig_in/docTest/doc.name">
        <field_name>doc.name</field_name>
        <field_type>string</field_type>
        <field_dim>0</field_dim>
      </record>
    </record>
    <record path="sig_in/docListTest">
      <field_name>docListTest</field_name>
      <field_type>record</field_type>
      <field_dim>1</field_dim>
      <record path="sig_in/docListTest/d0">
        <field_name>d0</field_name>
        <field_type>record</field_type>
        <field_dim>0</field_dim>
        <record path="sig_in/docListTest/d0/d0.name">
          <field_name>d0.name</field_name>
          <field_type>string</field_type>
          <field_dim>0</field_dim>
        </record>
      </record>
    </record>
    <record path="sig_in/packages_should_work">
      <field_name>packages_should_work</field_name>
      <field_type>recref</field_type>
      <field_dim>0</field_dim>
    </record>
    <record path="sig_in/packages_list_should_work">
      <field_name>packages_list_should_work</field_name>
      <field_type>recref</field_type>
      <field_dim>1</field_dim>
    </record>
  </Inputs>
  <Outputs name="sig_out">
    <record path="sig_out/doc">
      <field_name>doc</field_name>
      <field_type>string</field_type>
      <field_dim>0</field_dim>
    </record>
    <record path="sig_out/lDoc">
      <field_name>lDoc</field_name>
      <field_type>string</field_type>
      <field_dim>1</field_dim>
    </record>
    <record path="sig_out/docR">
      <field_name>docR</field_name>
      <field_type>string</field_type>
      <field_dim>0</field_dim>
    </record>
    <record path="sig_out/lDocR">
      <field_name>lDocR</field_name>
      <field_type>string</field_type>
      <field_dim>1</field_dim>
    </record>
  </Outputs>
</Values>

注意:这与您的预期输出仍有两个差异(我没有找到如何构建它们的合理信息):
* 输出中没有 field_dimension。
* 缺少拖车记录
<record path = "sig_in/packages_list_should_work/data:packages" />

于 2013-05-08T09:17:20.900 回答
0

很难判断这是否解决了您所描述的问题,但在上述 XSLT 片段的中间,

<xsl:attribute name="name">
  <xsl:value-of
    select="concat($parent_node, '/', self::node[@name]/@name)" />
</xsl:attribute>

您可以尝试将select属性更改为:

    select="concat($parent_node, '/', data)" />

这似乎太明显了,所以也许我错过了一些东西。

于 2013-05-07T20:04:15.183 回答