XSL LookUp打破了内部引用

时间:2018-06-13 12:07:58

标签: java xslt

我正在开发一个XSLT以从相同的XML文件获取内部引用,并从另一个Property.xml文件中查找以使用JAVA生成CSV文件。我面临的挑战是,内部引用似乎只能在查找嵌入到XSL中之前按预期工作。

XML

<?xml version="1.0" encoding="UTF-8"?>
<repository>
<types>
    <entity-type identifier="ArticleType" identifying-field-type-ref="ArticleType.Id">
        <object-name>article</object-name>
        <class-name>ArticleType</class-name>
        <persistence-xpath>/Article</persistence-xpath>
        <persistence-class-name>db.model.Article</persistence-class-name>
        <lower-bound>1</lower-bound>
        <upper-bound>1</upper-bound>
        <field-type identifier="ArticleType.AclFlag">
            <object-name>aclFlag</object-name>
            <class-name>commons.AclFlags</class-name>
            <persistence-xpath>/aclFlag</persistence-xpath>
            <persistence-class-name>java.lang.Integer</persistence-class-name>
            <fragment-column-access>Article.AclFlag</fragment-column-access>
            <internal>true</internal>
            <lower-bound>0</lower-bound>
            <range-min></range-min>
            <range-max></range-max>
            <min-length>0</min-length>
        </field-type>
    </entity-type>
</types>
<custom>
    <category identifier="master-data" order="1">
        <name>%category.MasterData</name>
    </category>
    <enum identifier="Enum.Acls">
        <name>%enum.Acls.name</name>
        <description>%enum.Acls.description</description>
        <class-name>com.heiler.ppm.repository.enumerations.StdEnumProvider</class-name>
        <key-class-name>commons.AclFlags</key-class-name>
        <entry label="%enum.Acls.entry.0" external-code="0" key="0" />
        <entry label="%enum.Acls.entry.1" external-code="1" key="1" />
        <entry label="%enum.Acls.entry.2" external-code="2" key="2" />
        <entry label="%enum.Acls.entry.3" external-code="3" key="3" />
        <entry label="%enum.Acls.entry.4" external-code="4" key="4" />
        <entry label="%enum.Acls.entry.5" external-code="5" key="5" />
        <entry label="%enum.Acls.entry.6" external-code="6" key="6" />
    </enum>
    <entity entity-type-ref="ArticleType" identifier="Article" parentEntities-ref="Product2G Variant">
        <name>%entity.Article.name</name>
        <description>%entity.Article.description</description>
        <label-pattern-short>{Article.SupplierAID}</label-pattern-short>
        <label-pattern-long>{Article.SupplierAID} - {ArticleLang.DescriptionShort}</label-pattern-long>
        <label-pattern-description>{ArticleLang.DescriptionLong}</label-pattern-description>
        <field identifier="Article.AclFlag" category-ref="master-data" enum-ref="Enum.Acls" field-type-ref="ArticleType.AclFlag">
            <name>%field.Article.AclFlag.name</name>
            <description>%field.Article.AclFlag.description</description>
            <editable>false</editable>
            <visible>true</visible>
            <visible-from-top>true</visible-from-top>
            <help-context></help-context>
            <mergeable>false</mergeable>
        </field>
    </entity>
</custom>
</repository>

Property.XML

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Default</comment>
<entry key="field.Article.AclFlag.name">Object right type</entry>
<entry key="category.MasterData">Header data</entry>
<entry key="enum.Acls.name">Object right types</entry>
</properties>

XSL

 <?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 exclude-result-prefixes="xs"
 version="1.0">

 <xsl:strip-space elements="*"/>
 <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>

 <xsl:key name="kEntity" match="entity-type" use="@identifier" />
 <xsl:key name="kCategory" match="category" use="@identifier" />
 <xsl:key name="kFieldID" match="field-type" use="@identifier" />
 <xsl:key name="kEnumID" match="enum" use="@identifier" />
 <xsl:key name="k1" match="entry" use="@key"/>

 <xsl:variable name="map-doc" select="document('../transform/Properties.xml')"/>

  <xsl:template match="/"><xsl:text>FieldIdentifier|FieldName|FieldEditable| FieldVisible|FieldVisibleFromTop|FieldMaxLength|FieldExportPurpose|FieldImportPurpose|FieldUpperBound|FieldAverageLength|FieldActive|FieldMultiline|FieldDisplayByDefault|FieldRichtext|FieldValue|CategoryIdentifier|CategoryName|FieldType|FieldProxyIdentifier|FieldLowerBound|FieldRangeMin|FieldRangeMax|EnumIdentifier|EnumName|EnumLabel|EnumExternalCode|EnumKey|EntityIDentifier|EntityName|EntityParentRef</xsl:text><xsl:text>&#xa;</xsl:text>
 <xsl:apply-templates select="repository/custom/entity/field"/>
 </xsl:template>

 <xsl:template match="field">

 <xsl:copy>
    <field>

    <xsl:value-of select="@identifier"/>
    <xsl:text>|</xsl:text>


 <xsl:variable name="key-value" select="substring-after(name,'%')"/>
 <xsl:for-each select="$map-doc">
 <xsl:value-of select="key('k1', $key-value)"/>
 </xsl:for-each>


    <xsl:text>|</xsl:text>
    <xsl:value-of select="editable"/>
    <xsl:text>|</xsl:text>
    <xsl:value-of select="visible"/>
    <xsl:text>|</xsl:text>
    <xsl:value-of select="visible-from-top"/>
    <xsl:text>|</xsl:text>
    <xsl:value-of select="max-length"/>
    <xsl:text>|</xsl:text>
    <xsl:value-of select="exportPurpose"/>
    <xsl:text>|</xsl:text>
    <xsl:value-of select="importPurpose"/>
    <xsl:text>|</xsl:text>
    <xsl:value-of select="upper-bound"/>
    <xsl:text>|</xsl:text>
    <xsl:value-of select="average-length"/>
    <xsl:text>|</xsl:text>
    <xsl:value-of select="active"/>
    <xsl:text>|</xsl:text>
    <xsl:value-of select="multiline"/>
    <xsl:text>|</xsl:text>
    <xsl:value-of select="display-by-default"/>
    <xsl:text>|</xsl:text>
    <xsl:value-of select="richtext"/>
    <xsl:text>|</xsl:text>
    <xsl:value-of select="value"/>
    <xsl:text>|</xsl:text>
    </field>
    <category-ref-name>
        <xsl:value-of select="key('kCategory', @category-ref)/@identifier"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="key('kCategory', @category-ref)/name"/>
        <xsl:text>|</xsl:text>
    </category-ref-name>
    <field-type-ref>
        <xsl:value-of select="substring-after(key('kFieldID', @field-type-ref)/persistence-class-name, 'java.lang.')"/>
        <xsl:text>|</xsl:text>
    </field-type-ref>
    <proxy-entity-ref>
        <xsl:value-of select="key('kFieldID', @field-type-ref)/@proxy-ref"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="key('kFieldID', @field-type-ref)/lower-bound"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="key('kFieldID', @field-type-ref)/range-min"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="key('kFieldID', @field-type-ref)/range-max"/>
        <xsl:text>|</xsl:text>
    </proxy-entity-ref>
    <enum-ref-name>
        <xsl:value-of select="key('kEnumID', @enum-ref)/@identifier"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="key('kEnumID', @enum-ref)/name"/>
        <xsl:text>|</xsl:text>
         <xsl:for-each select="key('kEnumID', @enum-ref)/entry">
                <xsl:value-of select="@label"/>
                <xsl:text>;</xsl:text>
        </xsl:for-each>
        <xsl:text>|</xsl:text>
         <xsl:for-each select="key('kEnumID', @enum-ref)/entry">
                <xsl:value-of select="@external-code"/>
                <xsl:text>;</xsl:text>
        </xsl:for-each>
        <xsl:text>|</xsl:text>
         <xsl:for-each select="key('kEnumID', @enum-ref)/entry">
                <xsl:value-of select="@key"/>
                <xsl:text>;</xsl:text>
        </xsl:for-each>
        <xsl:text>|</xsl:text>


    </enum-ref-name>
    <entity>
        <xsl:value-of select="../@identifier"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="../name"/>
        <xsl:text>|</xsl:text>
        <xsl:value-of select="../@parentEntities-ref"/>

    </entity>
  </xsl:copy>
  <xsl:text>&#xa;</xsl:text>
 </xsl:template>
 </xsl:stylesheet>

Output将查找添加到XSL 之前   FieldIdentifier|FieldName|FieldEditable|FieldVisible|FieldVisibleFromTop|FieldMaxLength|FieldExportPurpose|FieldImportPurpose|FieldUpperBound|FieldAverageLength|FieldActive|FieldMultiline|FieldDisplayByDefault|FieldRichtext|FieldValue|CategoryIdentifier|CategoryName|FieldType|FieldProxyIdentifier|FieldLowerBound|FieldRangeMin|FieldRangeMax|EnumIdentifier|EnumName|EnumLabel|EnumExternalCode|EnumKey|EntityIDentifier|EntityName|EntityParentRef Article.AclFlag|%field.Article.AclFlag.name|false|true|true|||||||||||master-data|%category.MasterData|Integer||0|||Enum.Acls|%enum.Acls.name|%enum.Acls.entry.0;%enum.Acls.entry.1;%enum.Acls.entry.2;%enum.Acls.entry.3;%enum.Acls.entry.4;%enum.Acls.entry.5;%enum.Acls.entry.6;|0;1;2;3;4;5;6;|0;1;2;3;4;5;6;|Article|%entity.Article.name|Product2G Variant

将查找添加到XSL后输出     FieldIdentifier|FieldName|FieldEditable|FieldVisible|FieldVisibleFromTop|FieldMaxLength|FieldExportPurpose|FieldImportPurpose|FieldUpperBound|FieldAverageLength|FieldActive|FieldMultiline|FieldDisplayByDefault|FieldRichtext|FieldValue|CategoryIdentifier|CategoryName|FieldType|FieldProxyIdentifier|FieldLowerBound|FieldRangeMin|FieldRangeMax|EnumIdentifier|EnumName|EnumLabel|EnumExternalCode|EnumKey|EntityIDentifier|EntityName|EntityParentRef Article.AclFlag|Object right type|false|true|true|||||||||||||||||||||||Article|%entity.Article.name|Product2G Variant

预期输出 FieldIdentifier|FieldName|FieldEditable|FieldVisible|FieldVisibleFromTop|FieldMaxLength|FieldExportPurpose|FieldImportPurpose|FieldUpperBound|FieldAverageLength|FieldActive|FieldMultiline|FieldDisplayByDefault|FieldRichtext|FieldValue|CategoryIdentifier|CategoryName|FieldType|FieldProxyIdentifier|FieldLowerBound|FieldRangeMin|FieldRangeMax|EnumIdentifier|EnumName|EnumLabel|EnumExternalCode|EnumKey|EntityIDentifier|EntityName|EntityParentRef Article.AclFlag|Object right type|false|true|true|||||||||||master-data|Header data|Integer||0|||Enum.Acls|Object right types|%enum.Acls.entry.0;%enum.Acls.entry.1;%enum.Acls.entry.2;%enum.Acls.entry.3;%enum.Acls.entry.4;%enum.Acls.entry.5;%enum.Acls.entry.6;|0;1;2;3;4;5;6;|0;1;2;3;4;5;6;|Article|%entity.Article.name|Product2G Variant

0 个答案:

没有答案