-1

我有一个输入 XML 如下

输入 XML

<description-page>
    <noted>12000 </noted>
    <noted>15000</noted>
    <noted>NOTE</noted>
</description-page>
<idescription-note>
<noted>12000</noted>
<noted>15000</noted>
<noted>ENG CHG</noted>
</idescription-note>

我想我的输出为

<sample>
<input>
    <noted>12000</noted>
    <noted>12000</noted>
</input>
<input>
    <noted>15000</noted>
    <noted>15000</noted>
</input>
<input>
    <noted>NOTE</noted>
    <noted>ENG CHG</noted>
</input>
</sample>

所以这里每个 description-page (noted) 都需要 idescription-note (noted) 元素

我现在正在做的是在 xslt

<xsl-template match="description-page | idescription-note>

这就是我尝试 xslt 的方式,但我并没有在如何匹配两个节点方面遇到困难。

请在这里指导我。

问候卡西克

4

2 回答 2

0

您可以使用来同步和节点position()的相对位置,如下所示:description-pageidescription-note

<xsl:template match="description-page">
    <sample>
        <xsl:for-each select="noted">
            <input>
                <xsl:variable name="position" select="position()" />
                <noted>
                    <xsl:value-of select="//description-page/noted[$position]/text()"/>
                </noted>
                <noted>
                    <xsl:value-of select="//idescription-note/noted[$position]/text()"/>
                </noted>
            </input>
        </xsl:for-each>
    </sample>
</xsl:template>

编辑以下内容也可以在没有 for-each 的情况下实现这一点

<xsl:template match="description-page">
    <sample>
        <xsl:apply-templates select="noted"/>
    </sample>
</xsl:template>

<xsl:template match="noted">
    <input>
        <xsl:variable name="position" select="position()" />
        <noted>
            <xsl:value-of select="./text()"/>
        </noted>
        <noted>
            <xsl:value-of select="//idescription-note/noted[$position]/text()"/>
        </noted>
    </input>
</xsl:template>

<!--Suppress the description-page tree entirely-->
<xsl:template match="idescription-note">
</xsl:template>

我没有尝试过使用大型 xml,但使用了您的示例

  • foreach:样式表执行时间:94.91 毫秒
  • 应用模板:样式表执行时间:94.93 毫秒

这么多?性能方面的一件事是将//idescription-note路径替换为该节点的真实完整路径。

于 2012-07-19T12:57:20.830 回答
0

假设输入是

<root>
<description-page>
    <noted>12000 </noted>
    <noted>15000</noted>
    <noted>NOTE</noted>
</description-page>
<idescription-note>
<noted>12000</noted>
<noted>15000</noted>
<noted>ENG CHG</noted>
</idescription-note>
</root>

然后是样式表

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>

<xsl:variable name="id-notes" select="//idescription-note/noted"/>

<xsl:template match="root">
  <sample>
    <xsl:apply-templates select="description-page/noted"/>
  </sample>
</xsl:template>

<xsl:template match="description-page/noted">
  <input>
    <xsl:copy-of select="."/>
    <xsl:variable name="pos" select="position()"/>
    <xsl:copy-of select="$id-notes[$pos]"/>
  </input>
</xsl:template>

</xsl:stylesheet>

输出

<sample>
   <input>
      <noted>12000 </noted>
      <noted>12000</noted>
   </input>
   <input>
      <noted>15000</noted>
      <noted>15000</noted>
   </input>
   <input>
      <noted>NOTE</noted>
      <noted>ENG CHG</noted>
   </input>
</sample>
于 2012-07-19T13:08:36.373 回答