0

我知道关于这个主题还有其他问题,但我正在寻找它不存在。

我想要做的是组合 2 个 XML 文件,但不完全是一个接一个的 XML 文件。

例如,我的第一个 xml 文件是 game.xml:

<forSale>
<game>
    <title>Mass Effect 3</title>
    <releaseDate>
        <yyyy>2012</yyyy>
        <mm>03</mm>
        <dd>06</dd>
    </releaseDate>
    <esrbRating>M</esrbRating>
    <platforms>
        <platform>X360</platform>
    </platforms>
</game>
<game>
    <title>Borderlands 2</title>
    <releaseDate>
        <yyyy>2012</yyyy>
        <mm>09</mm>
        <dd>18</dd>
    </releaseDate>
    <esrbRating>M</esrbRating>
    <platforms>
        <platform>PC</platform>
    </platforms>
</game>

我的第二个xml文件reviews.xml

<reviews>
<game>
    <title>Mass Effect 3</title>
    <review>
        <critic>Kevin VanOrd</critic>
        <pros>
            <pro><![CDATA[Fantastic, moving story that balances plot and character]]></pro>
        </pros>
        <cons>
            <con><![CDATA[Some glitches and bugs]]></con>
        </cons>
    </review>
</game>

<game>
    <title>Borderlands 2</title>
    <review>
        <critic>Chris Watters</critic>
        <pros>
            <pro><![CDATA[A ton of great mission writing and dialogue]]></pro>
        </pros>
        <cons>
            <con><![CDATA[Spoken messages sometimes interrupt each other]]></con>
        </cons>
    </review>
</game>

我希望我生成的主 XML 是这样的:

<forSale>
<game>
    <title>Mass Effect 3</title>
    <releaseDate>
        <yyyy>2012</yyyy>
        <mm>03</mm>
        <dd>06</dd>
    </releaseDate>
    <esrbRating>M</esrbRating>
    <platforms>
        <platform>X360</platform>
    </platforms>
    <review>
        <critic>Kevin VanOrd</critic>
        <synopsis>
            Mass Effect 3 is a remarkably satisfying conclusion to a beloved trilogy, and a poignant and memorable role-playing action game in its own right.
        </synopsis>
        <pros>
            <pro>Fantastic, moving story that balances plot and character</pro>
        </pros>
        <cons>
            <con>Some glitches and bugs</con>
        </cons>
    </review>
</game>
<game>
    <title>Borderlands 2</title>
    <releaseDate>
        <yyyy>2012</yyyy>
        <mm>09</mm>
        <dd>18</dd>
    </releaseDate>
    <esrbRating>M</esrbRating>
    <platforms>
        <platform>PC</platform>
    </platforms>
    <review>
        <critic>Chris Watters</critic>
        <synopsis>
            Stellar writing and a host of small improvements help Borderlands 2 stand tall on the shoulders of its predecessor.
        </synopsis>
        <pros>
            <pro>A ton of great mission writing and dialogue</pro>
        </pros>
        <cons>
            <con>Spoken messages sometimes interrupt each other</con>
        </cons>
    </review>
</game>

我试过这段代码,但它首先输出游戏然后是评论

<xsl:template match="/forSale">
<xsl:copy>
    <xsl:apply-templates select="game"/>
    <xsl:apply-templates select="document('reviews.xml')/reviews/game/review"/>
    </xsl:copy>
</xsl:template>
<xsl:template match =" @* | node()">
<xsl:copy>
  <xsl:apply-templates select="@* | node() | text()"/>
</xsl:copy>
</xsl:template>
4

1 回答 1

1

这是因为这条线...

<xsl:apply-templates select="document('reviews.xml')/reviews/game/review"/>

独立于您选择游戏的前一行,因此将复制所有游戏,而不仅仅是特定行。

你需要做的就是多将这一行变成一个匹配游戏元素的模板,并修改为只输出选中的游戏

<xsl:apply-templates select="document('reviews.xml')/reviews/game[title=current()/title]/review"/>

试试这个 XSLT

<xsl:template match="/forSale">
   <xsl:copy>
       <xsl:apply-templates select="game"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="game">
   <xsl:copy>
       <xsl:apply-templates select="@* | node() | text()"/>
       <xsl:apply-templates select="document('reviews.xml')/reviews/game[title=current()/title]/review"/>
   </xsl:copy>
</xsl:template>

<xsl:template match =" @* | node()">
   <xsl:copy>
     <xsl:apply-templates select="@* | node() | text()"/>
   </xsl:copy>
</xsl:template>
于 2013-05-20T12:31:23.570 回答