在XSLT中连接节点值

时间:2014-12-16 08:36:40

标签: xml xslt

我需要根据以下内容连接XML的值:

这是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Parent>
    <Name>Father 1</Name>
    <Child>
      <Name>F1 - Child 1</Name>
      <Age>2</Age>
    </Child>
    <Child>
      <Name>F1- Child 2</Name>
      <Age>4</Age>
    </Child>
  </Parent>
  <Parent>
    <Name>Father 2</Name>
    <Child>
      <Name>F2 - Child 1</Name>
      <Age>2</Age>
    </Child>
    <Child>
      <Name>F2 - Child 2</Name>
      <Age>4</Age>
    </Child>
  </Parent>
</Root>

这是我的XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match ="Root">
    <xsl:apply-templates select ="Parent/Name" />
    <xsl:apply-templates select ="Parent/Child" />

  </xsl:template>

  <xsl:template match ="Parent/Name">
    <FatherName>
      <xsl:value-of select ="."/>
    </FatherName>
  </xsl:template>

  <xsl:template match ="Parent/Child">
    <ChildNames>
      <xsl:for-each select="Name">
        <xsl:value-of select="." />
        <xsl:if test="position()!=last()">
          <xsl:text>, </xsl:text>
        </xsl:if>
      </xsl:for-each>
    </ChildNames>
  </xsl:template>

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

这导致了这个

<?xml version="1.0" encoding="utf-8"?>
<FatherName>Father 1</FatherName>
<FatherName>Father 2</FatherName>
<ChildNames>F1 - Child 1</ChildNames>
<ChildNames>F1- Child 2</ChildNames>
<ChildNames>F2 - Child 1</ChildNames>
<ChildNames>F2 - Child 2</ChildNames>

但我需要的结果是这样的:

<FatherName>Father 1</FatherName>
<ChildNames>F1 - Child 1, F1- Child 2</ChildNames>
<FatherName>Father 2</FatherName>
<ChildNames>F2 - Child 1, F2 - Child 2</ChildNames>

任何人都可以帮助我获得我需要的结果吗?

2 个答案:

答案 0 :(得分:3)

基本上,您需要做的是将行<xsl:apply-templates select ="Parent/Child" />移出当前模板,然后移到与Parent/Name匹配的模板中。像这样:

<xsl:template match="Parent/Name">
  <FatherName>
    <xsl:value-of select ="."/>
  </FatherName>
  <ChildNames>
      <xsl:apply-templates select ="../Child" />
  </ChildNames>
</xsl:template>

这里的..将返回到Parent节点,以便您可以选择子节点。请注意,您也不需要模板匹配子项中的xsl:for-each

实际上,Parent节点上的匹配可能比Parent/name节点更容易。

试试这个XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="Root">
    <xsl:apply-templates select="Parent" />
  </xsl:template>

  <xsl:template match="Parent">
    <FatherName>
      <xsl:value-of select ="Name"/>
    </FatherName>
    <ChildNames>
       <xsl:apply-templates select ="Child" />
    </ChildNames>
  </xsl:template>

  <xsl:template match="Child">
      <xsl:if test="position()!=1">
        <xsl:text>, </xsl:text>
      </xsl:if>
      <xsl:value-of select="Name" />
  </xsl:template>

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

答案 1 :(得分:0)

想出了这个

  <xsl:template match="/">
    <good>
      <xsl:for-each select="Root/Parent">
        <FatherName>
          <xsl:value-of select="Name"/>
        </FatherName>
        <xsl:variable name="childCount" select="count(.//Child)"/>
        <ChildNames>
          <xsl:variable name="data">
            <xsl:for-each select=".//Child">
              <xsl:value-of select="Name"/>
              <xsl:if test="not(position()=$childCount)">
                <xsl:text>, </xsl:text>
              </xsl:if>
            </xsl:for-each>
          </xsl:variable>
          <xsl:value-of select="$data"/>
        </ChildNames>
      </xsl:for-each>
    </good>
  </xsl:template>
相关问题