使用集合的XSLT转换缺少上下文项

时间:2013-05-26 10:30:51

标签: xml xslt xslt-2.0 saxon

我想使用XSLT读取子目录中的所有XML文件并将结果输出到单个XML文件,但是我收到一条错误消息,指出上下文项不存在。如果XSL一次应用于一个文件,则它可以工作,但是在使用Collection函数时我无法使用它。

这是XSL:

<xsl:template match="node()|@*" mode="inFile">
             <xsl:copy>
                  <xsl:apply-templates mode="inFile" select="node()|@*"/>
             </xsl:copy>
        </xsl:template>

<xsl:template name="main" match="/">
    <xsl:apply-templates mode="inFile" select="collection('file:///C:/2011 Valid XML/XML?select=*.xml;recurse=yes')"/>
    <LookupTables>
        <Facility_Identifiers>
            <xsl:for-each select="/Report/RegistrationData/Facility/Identifiers/IdentifierList">
                <Facility_Identifier Org="{/Report/RegistrationData/Organisation/Details/BusinessLegalName}" Facility="{/Report/RegistrationData/Facility/Details/FacilityName}" NPRI_ID="{Identifier[IdentifierType = 'NationalPollutantReleaseInventoryId']/IdentifierValue}" GHGIS_ID="{Identifier[IdentifierType = 'GHGRPId']/IdentifierValue}"/>
            </xsl:for-each>
        </Facility_Identifiers>
        <NAICS_Details>
            <xsl:for-each select="/Report/RegistrationData/Facility/Identifiers/NAICSCodeList/NAICSCode">
                <NAICS_Detail Org="{/Report/RegistrationData/Organisation/Details/BusinessLegalName}" Facility="{/Report/RegistrationData/Facility/Details/FacilityName}" NAICS="{Code}" Classification="{NAICSClassification}" Percentage="{ActivityPercentage}"/>
            </xsl:for-each>
        </NAICS_Details>
    </LookupTables>
</xsl:template>

源XML文件如下所示:

<Report xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
    <RegistrationData>  
        <Facility>  
            <Details>  
                <FacilityName>Random Location</FacilityName>  
            </Details>  
            <Identifiers>  
                <IdentifierList>  
                    <Identifier>  
                        <IdentifierType>NationalPollutantReleaseInventoryId</IdentifierType>  
                        <IdentifierValue>N999</IdentifierValue>  
                    </Identifier>  
                    <Identifier>  
                        <IdentifierType>GHGRPId</IdentifierType>  
                        <IdentifierValue>G77777</IdentifierValue>  
                    </Identifier>  
                </IdentifierList>  
                <NAICSCodeList>  
                    <NAICSCode>  
                        <NAICSClassification>205</NAICSClassification>  
                        <Code>322121</Code>  
                        <ActivityPercentage>100.00</ActivityPercentage>  
                    </NAICSCode>  
                    <NAICSCode>  
                        <NAICSClassification>81</NAICSClassification>  
                        <Code>221112</Code>  
                        <ActivityPercentage>90.00</ActivityPercentage>  
                    </NAICSCode>  
                </NAICSCodeList>  
            </Identifiers>  
        </Facility>  
        <Organisation>  
            <Details>  
                <BusinessLegalName>The Corporation</BusinessLegalName>  
            </Details>  
        </Organisation>  
    </RegistrationData>  
</Report>  

我希望目标XML文件看起来像这样:

<LookupTables>  
    <Facility_Identifiers>  
        <Facility_Identifier Org="The Corporation" Facility="Random Location" NPRI_ID="N999" GHGIS_ID="G77777"/>  
    </Facility_Identifiers>  
    <NAICS_Details>  
        <NAICS_Detail Org="The Corporation" Facility="Random Location" NAICS="322121" Classification="205" Percentage="100.00"/>  
        <NAICS_Detail Org="The Corporation" Facility="Random Location" NAICS="221112" Classification="81" Percentage="90.00"/>  
    </NAICS_Details>  
</LookupTables>

1 个答案:

答案 0 :(得分:0)

如果要编译所有文件中的数据,请使用

之类的方法
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>

<xsl:param name="input-dir" select="'file:///C:/2011 Valid XML/XML'"/>

<xsl:template name="main">
  <LookupTables>
        <xsl:variable name="input-docs"
          select="collection(iri-to-uri(concat($input-dir, '?select=*.xml')))"/>
        <Facility_Identifiers>
            <xsl:for-each select="$input-docs/Report/RegistrationData/Facility/Identifiers/IdentifierList">
                <Facility_Identifier Org="{ancestor::RegistrationData/Organisation/Details/BusinessLegalName}" 
                Facility="{ancestor::RegistrationData/Facility/Details/FacilityName}" 
                NPRI_ID="{Identifier[IdentifierType = 'NationalPollutantReleaseInventoryId']/IdentifierValue}" GHGIS_ID="{Identifier[IdentifierType = 'GHGRPId']/IdentifierValue}"/>
            </xsl:for-each>
        </Facility_Identifiers>
        <NAICS_Details>
            <xsl:for-each select="$input-docs/Report/RegistrationData/Facility/Identifiers/NAICSCodeList/NAICSCode">
                <NAICS_Detail Org="{ancestor::RegistrationData/Organisation/Details/BusinessLegalName}" 
                Facility="{ancestor::RegistrationData/Facility/Details/FacilityName}" NAICS="{Code}" 
                Classification="{NAICSClassification}" Percentage="{ActivityPercentage}"/>
            </xsl:for-each>
        </NAICS_Details>  </LookupTables>
</xsl:template>

</xsl:stylesheet>