XSLT中的不同值

时间:2019-07-18 13:23:30

标签: xml xslt

我已经编写了xslt代码以将XML代码转换为所需的格式。但是父节点每次都在子节点之后重复。我想先显示父节点,然后再显示其子节点。请让我知道我该怎么做才能获得所需的输出。

Transform.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 xmlns:app="http://www.eee.com/app">
  <xsl:output method="text" />
  <xsl:strip-space elements="*" />

  <xsl:template match="//*">
    <xsl:param name="prefix" />
    <xsl:param name="inLast" select="true()" />

<xsl:value-of select="$prefix"/>
  <xsl:text>&#xA;</xsl:text>
    <xsl:value-of select="concat( local-name(), ' ', normalize-space())"/>
    <xsl:if test="not($inLast) or position() != last()">
      <xsl:text>&#xA;</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template match="//*[*]">

    <xsl:param name="inLast" select="true()" />
    <xsl:variable name="num">
      <xsl:number />
    </xsl:variable>


    <xsl:apply-templates>
      <xsl:with-param name="prefix" select="local-name()" />
      <xsl:with-param name="inLast" select="$inLast and position() = last()" />
    </xsl:apply-templates>


  </xsl:template>
</xsl:stylesheet>

Input.xml

<?xml version="1.0" encoding="UTF-8"?>
<ns0:LogDeliveryDocumentNotification xmlns:ns0="http://fmc.fmcworld.com/pi/MTD/LogDelivery">
<Employee1>
<Name>ABC</Name>
<Age>25</Age>
</Employee1>
<Employee2>
<Name>DEF</Name>
<Age>26</Age>
</Employee2>
</ns0:LogDeliveryDocumentNotification>

输出:

Employee1
Name ABC
Employee1
Age 25
Employee2
Name DEF
Employee2
Age 26

我希望输出看起来像这样:

Employee1
Name ABC
Age 25
Employee2
Name DEF
Age 26

1 个答案:

答案 0 :(得分:1)

您的输入中没有重复的值,因此无需提取不同的值。您想要的结果可以通过以下方式轻松产生:

XSLT 1.0

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

<xsl:template match="/*">
    <xsl:for-each select="*">
        <xsl:value-of select="name()" />
        <xsl:text>&#10;</xsl:text>
            <xsl:for-each select="*">   
                <xsl:value-of select="name()" />
                <xsl:text> </xsl:text>
                <xsl:value-of select="." />
                <xsl:text>&#10;</xsl:text>
            </xsl:for-each>
        </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

演示https://xsltfiddle.liberty-development.net/jyRYYjg/2

相关问题