簡體   English   中英

連接XSLT中的節點值

[英]Concatenate node values in 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>

誰能幫助我獲得所需的結果?

本質上,您需要做的是將<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/name節點相比,在“ Parent節點上進行匹配可能更容易。

試試這個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>

想出了這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM