2

私は XSL が初めてで、次のような場所が見つかりませんでした。source.xml を target.xml に変換したい。使用モード「グループ」がありますが、うまくいきませんでした(おそらく、適切に使用できませんでした)

ソース.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>

<PersonBody>

    <Person>
        <D>Name</D>
        <D>Surname</D>
        <D>Id</D>
    </Person>

    <PersonValues>
        <D>Michael</D>
        <D>Jackson</D>
        <D>01</D>
    </PersonValues>

    <PersonValues>
        <D>James</D>
        <D>Bond</D>
        <D>007</D>
    </PersonValues>

    <PersonValues>
        <D>Kobe</D>
        <D>Bryant</D>
        <D>24</D>
    </PersonValues>

</PersonBody>

ターゲット.xml:

<PersonBody>
  <AllValues>
    <Name>
      <D>Michael</D>
      <D>James</D>
      <D>Kobe</D>
    </Name>
    <Surname>
      <D>Jackson</D>
      <D>Bond</D>
      <D>Bryant</D>
    </Surname>
    <Id>
      <D>01</D>
      <D>007</D>
      <D>24</D>
    </Id>
  </AllValues>
</PersonBody>

編集:出力が変更されたため、別の質問をしました。ここから他の質問を見つけることができます

4

1 に答える 1

1

これを試してください:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:key name="kColumnValue" match="PersonValues/*" 
           use="count(preceding-sibling::*)" />

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

  <xsl:template match="/*">
    <xsl:copy>
      <AllValues>
        <xsl:apply-templates select="Person/*" />
      </AllValues>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Person/*">
    <xsl:element name="{.}">
      <xsl:apply-templates select="key('kColumnValue', position() - 1)" />
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

サンプル XML で実行すると、結果は次のようになります。

<PersonBody>
  <AllValues>
    <Name>
      <D>Michael</D>
      <D>James</D>
      <D>Kobe</D>
    </Name>
    <Surname>
      <D>Jackson</D>
      <D>Bond</D>
      <D>Bryant</D>
    </Surname>
    <Id>
      <D>01</D>
      <D>007</D>
      <D>24</D>
    </Id>
  </AllValues>
</PersonBody>
于 2013-04-24T08:57:04.507 に答える