【问题标题】:XSLT Duplicating node & all child nodes & attributes with new valuesXSLT 复制节点和所有子节点以及具有新值的属性
【发布时间】:2015-04-14 18:15:23
【问题描述】:

美好的一天.... 我正在尝试使用更新/新元素文本和/或属性值复制节点。

我的输入 XML 文件:

<?xml version="1.0"?>
<products author="Jesper">
  <product id="p1">
    <name>Delta</name>
    <price>800</price>
    <stock>4</stock>
    <country>Denmark</country>
  </product>
</products>

所需的 XML 输出:

<?xml version="1.0" encoding="utf-8"?>
<products author="Jesper">
  <product id="p1">
    <name>Delta</name>
    <price>800</price>
    <stock>4</stock>
    <country>Denmark</country>
  </product>
  <product id="NEW_p1">
    <name>NEW_Delta</name>
    <price>NEW_800</price>
    <stock>NEW_4</stock>
    <country>NEW_Denmark</country>
  </product>
</products>

一段时间后,我目前拥有的 XSLT 如下:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.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="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match ="product">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    <product>
      <xsl:attribute name ="id">
        <xsl:value-of select ="concat('NEW_',@id"/>
      </xsl:attribute>
      <xsl:copy>
        <xsl:apply-templates select="node()"/>
      </xsl:copy>
    </product>
  </xsl:template>

但是,使用上述转换,我得到以下 XML 输出:

<?xml version="1.0" encoding="utf-8"?>
<products author="Jesper">
  <product id="p1">
    <name>Delta</name>
    <price>800</price>
    <stock>4</stock>
    <country>Denmark</country>
  </product>
  <product id="NEW_p1"><product>
    <name>Delta</name>
    <price>800</price>
    <stock>4</stock>
    <country>Denmark</country>
  </product></product>
</products>

如您所见,product 元素被添加,同时我声明了一个具有新 @id 值的新 product 元素。由于我用来处理子节点,我相信这会再次处理 product 元素。

另外,我需要帮助来更新子节点的值(在每个值前面加上“NEW_”)。搜索这个网站上的大量问题,我相信我需要一个这样的模板:

<xsl:template match="*">
  <xsl:element name ="{local-name()}">
    <!--for all attributes-->
    <xsl:copy-of select ="@*"/>
    <xsl:value-of select = "."/>
  </xsl:element>
</xsl:template>

提前感谢您对我的问题提出任何建议/想法。

更新 感谢@Mathias 对我最初的问题的回答。所提供的答案又带来了一个问题,涉及到 XML 结构的更深层次的递归。

输入 XML 文件:

    <products author="Jesper">
      <product id="p1">
        <name>Delta
          <innerName>MiddleDelta
            <baseName>FinalDelta</baseName>
          </innerName>
        </name>
        <price>800</price>
        <stock>4</stock>
        <country>Denmark
          <city>Copenhagen</city>
        </country>
      </product>
    </products>

更新的欲望输出文件是这样的:

<?xml version="1.0" encoding="utf-8"?>
<products author="Jesper">
  <product id="p1">
    <name>Delta
      <innerName>MiddleDelta
        <baseName>FinalDelta</baseName>
      </innerName>
    </name>
    <price>800</price>
    <stock>4</stock>
    <country>Denmark
      <city>Copenhagen</city>
    </country>
  </product>
  <product id="NEW_p1">
    <name>NEW_Delta
      <innerName>NEW_MiddleDelta
        <baseName>NEW_FinalDelta</baseName>
      </innerName>
    </name>
    <price>NEW_800</price>
    <stock>NEW_4</stock>
    <country>NEW_Denmark
      <city>NEW_Copenhagen</city>
    </country>
  </product>
</products>

我只能猜测使用模板会起作用,因为每个节点都有不同级别的子节点。提前感谢您对此提出的想法/建议。

【问题讨论】:

  • 你得到更深层递归的答案了吗?
  • @DanielHaley 不,我没有。

标签: xml xslt xslt-2.0 node-set


【解决方案1】:

这是对您更新的问题的回应(恕我直言,应该作为一个新问题提出):

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/products">
    <xsl:copy>
        <xsl:copy-of select="product"/>
        <xsl:apply-templates select="product"/>
    </xsl:copy>
</xsl:template>

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

<xsl:template match="@*">
    <xsl:attribute name="{name()}">
        <xsl:value-of select="concat('NEW_', .)"/>
    </xsl:attribute>
</xsl:template>

<xsl:template match="text()">
    <xsl:value-of select="concat('NEW_', .)"/>
</xsl:template>

</xsl:stylesheet>

【讨论】:

  • 我还要为匹配 text() 的模板添加优先级,这样您就不会收到可能的模棱两可的模板匹配警告(使用匹配 node() 的模板)。
  • @DanielHaley 谢谢。你指出问题是对的。不过,我选择了一个稍微不同的解决方案。
  • 谢谢@michael.hor257k。我得到的一件事是,在每个结束标记之后 我有文本 NEW_ 所以对于上面更新的输入文件,我有 ...NEW_ ...新_
  • 您的样式表的顶层是否有&lt;xsl:strip-space elements="*"/&gt;
  • @michael.hor257k 不,它没有。效果很好 - 再次感谢您!
【解决方案2】:

离你不远了,但主要有两个问题:

  • 您在第二个文字 product 元素中使用 xsl:copy,这会导致输出中出现额外的 product 元素
  • 您需要一种方法来查找 product 元素的所有子元素,再次输出它们并将“NEW_”添加到它们的文本内容中。

您确定应将version 属性设置为“2.0”吗?另外,我不确定这个练习的要点是什么......

XSLT 样式表

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match ="product">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    <product id="{concat('NEW_',@id)}">
      <xsl:for-each select="*">
          <xsl:copy>
              <xsl:value-of select="concat('NEW_',.)"/>
          </xsl:copy>
      </xsl:for-each>
    </product>
  </xsl:template>

</xsl:stylesheet>

XML 输出

<?xml version="1.0" encoding="UTF-8"?>
<products author="Jesper">
  <product id="p1">
      <name>Delta</name>
      <price>800</price>
      <stock>4</stock>
      <country>Denmark</country>
  </product>
   <product id="NEW_p1">
      <name>NEW_Delta</name>
      <price>NEW_800</price>
      <stock>NEW_4</stock>
      <country>NEW_Denmark</country>
   </product>
</products>

【讨论】:

  • 感谢您的回答。我了解到使用模板是转换 XML 时要走的路——我看到了 for-each 语法的使用。
  • @Lorentz for-each 和模板都有它们的位置 - 重要的是当另一个更好时你不要使用一个。 xsl:for-each 确实是一个被严重滥用的结构,主要是初学者。如果它解决了您的问题,请不要忘记accept this answer。谢谢!
  • 适当注意“for-each”的使用 - 在我前进的过程中,我会牢记您的 cmets。这个练习是一个更大项目的一部分——我只需要深入了解使用新数据复制节点的核心。此转换是否允许递归到 XML 输入文件中的更深层次?提前致谢。
  • @Lorentz 不,目前不会。一般来说,* 上的人只能使用显示给他们的 XML。您需要显示这些深层结构并解释您希望代码以何种方式递归运行。
  • 再次感谢您的回复/回答。
最近更新 更多