为什么xslt中的“for-each select”功能不打印元素名称和标签?

时间:2016-10-01 17:37:35

标签: xml xslt

我有以下源xml文件,xslt代码和输出xml

问题是我需要遍历节点JLine 使用:

xsl:for-each select="JLine"

哪个输出正确但标签丢失。 我需要实现循环,因为我将使用if条件来处理节点并消除一些不需要的元素。

XML输入

<?xml version="1.0" encoding="UTF-8"?>
        <JLine sequence="1">
            <Amount currencyID="USD">-700.000</Amount>
            <FunctionalAmount currencyID="USD">-700.000</FunctionalAmount>
            <ReportingCurrencyAmount currencyID="USD">-700.000</ReportingCurrencyAmount>
            <GLAccount>
                <GLNominalAccount>S1010053</GLNominalAccount>
                <AccountingChartReference>
                    <ID accountingEntity="T00">T00</ID>
                </AccountingChartReference>
            </GLAccount>
            <DimensionCodes>
                <DimensionCode sequence="1" listID="VAT">KTS08010</DimensionCode>
                <DimensionCode sequence="2" listID="FILE">KF86155281</DimensionCode>
            </DimensionCodes>
        </JLine>
                    <JLine sequence="2">
            <Amount currencyID="USD">-700.000</Amount>
            <FunctionalAmount currencyID="USD">50.000</FunctionalAmount>
            <ReportingCurrencyAmount currencyID="USD">400.000</ReportingCurrencyAmount>
            <GLAccount>
                <GLNominalAccount>S1010053</GLNominalAccount>
                <AccountingChartReference>
                    <ID accountingEntity="T00">T00</ID>
                </AccountingChartReference>
            </GLAccount>
            <DimensionCodes>
                <DimensionCode sequence="1" listID="VAT">mKTS08010</DimensionCode>
                <DimensionCode sequence="2" listID="FILE">eKF86155281</DimensionCode>
            </DimensionCodes>
        </JLine>

xlt code

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

    <xsl:for-each select="JLine"> 
     <xsl:variable name="var:v1" select="DimensionCodes/DimensionCode" /> 
    <xsl:if test="$var:v1">
 <xsl:apply-templates select="." />

    </xsl:if>
 </xsl:for-each>      

XML输出结果

<?xml version="1.0" encoding="UTF-8"?>
        <JLine sequence="1">
            <Amount currencyID="USD">-700.000</Amount>
            <FunctionalAmount currencyID="USD">-700.000</FunctionalAmount>
            <ReportingCurrencyAmount currencyID="USD">-700.000</ReportingCurrencyAmount>
            <GLAccount>
                <GLNominalAccount>S1010053</GLNominalAccount>
                <AccountingChartReference>
                    <ID accountingEntity="T00">T00</ID>
                </AccountingChartReference>
            </GLAccount>
            <DimensionCodes>
                <DimensionCode sequence="1" listID="VAT">KTS08010</DimensionCode>
                <DimensionCode sequence="2" listID="FILE">KF86155281</DimensionCode>
            </DimensionCodes>
        </JLine>

1 个答案:

答案 0 :(得分:0)

您在任何模板之外都有xsl:for-each条指令。这是不允许的,会导致错误(而不是您报告的结果)。

我猜(!)你要替换这部分:

<xsl:for-each select="JLine"> 
    <xsl:apply-templates select="." />
    <Amount>
        <xsl:value-of select="Amount"/>
    </Amount>
</xsl:for-each> 

使用:

<xsl:template match="JLine">
    <Amount>
        <xsl:value-of select="Amount"/>
    </Amount>
</xsl:template>