【问题标题】:Why does "for-each select" funtion in xslt not printing elements name and tags?为什么 xslt 中的“for-each select”功能不打印元素名称和标签?
【发布时间】:2016-10-01 17:37:35
【问题描述】:

我有以下,源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 代码

<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>

【问题讨论】:

  • 如果您 (a) 正确格式化代码并且 (b) 包含预期结果(作为代码),您的问题会更加清晰。将示例最小化为仅演示问题所必需的部分也是受欢迎的。
  • @michael.hor257k 我做了所有的事情来上传示例代码..但是格式化不起作用..代码上传本来是一个很好的功能

标签: xml xslt


【解决方案1】:

您在任何模板之外都有一个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>

【讨论】:

  • 亲爱的,我需要遍历 JLine 节点并执行“if”功能..您的答案不是使用 for-each 条件
  • 恐怕我不明白你的意思。 xsl:for-each 不是条件。
  • 对不起.. 我需要在 Node JLine 上执行 xsl:for-each 循环。如果 DimensionCodes/DimensionCode 不为空,则将映射 Jline 节点。如果不是,则忽略 JLine 节点
  • "我需要在 Node JLine 上执行 xsl:for-each 循环" 我不明白为什么,但是如果您这样做,则必须在模板中执行此操作.我无法进一步帮助您,因为您没有接受我的建议如何使您的问题更清楚。相反,您添加了另一段与您的问题无关的代码,只会造成混淆。
猜你喜欢
  • 2021-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-02
  • 2021-08-07
  • 2022-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多