xml - XSLT 更新仅给定属性的新值和 XPath 的属性

标签 xml xslt xpath

我有一些代码可以比较两个 XML 文档的属性差异(仅更新,而不是新的属性节点),并生成一组指向属性的 XPath 指针以及属性的新值。

设置

例如,给定旧的 XML 和新的 xml:

旧 XML

<EntityA>
  <EntityB id="foo1" value="bar1" ignoredbutsave="bazz1"/>
</EntityA>

新 XML

<EntityA>
  <EntityB id="foo2" value="bar2"/>
</EntityA>

我的代码将返回

/EntityA/EntityB/@id, foo2
/EntityA/EntityB/@value, bar2

我想生成一个 XSLT,将旧 XML 合并到新 XML 中,以创建以下 XML:

<EntityA>
  <EntityB id="foo2" value="bar2" ignoredbutsave="bazz1"/>
</EntityA>

我在 SO 上找到的所有答案都假设对属性名称有一些先验知识。在本例中,我仅获得属性的 XPath 引用,而不是名称本身。我知道我可以解析 XPath 字符串来派生属性名称,但更愿意将这种复杂性排除在代码之外。

我尝试过的

我无法使用 attribute value template因为我需要从旧 XML 中复制 ignoredbutsave 属性。我尝试使用 xsl:param 从 XPath 中选择属性名称并在 xsl:attribute 中使用它,如下所示:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes"/>

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

  <xsl:template match="/EntityA/EntityB/@id">
    <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
       <xsl:param name="newValue" select="name(/EntityA/EntityB/@id)"/>
         <xsl:attribute name="$newValue">newAttributeId</xsl:attribute>
     </xsl:copy>
  </xsl:template>

  <xsl:template match="/EntityA/EntityB/@value">
    <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
       <xsl:param name="myattrname" select="name(/EntityA/EntityB/@value)"/>
         <xsl:attribute name="$myattrname">newAttributeValue</xsl:attribute>
     </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

但是,这会导致错误属性“name”的值“$myattrname”不是有效的 QName。

所以,问题是给定属性的 XPath 和该属性的新值,如何生成更新该值而不显式引用属性名称的 XSLT?

最佳答案

此 XSLT 转换:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes"/>

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

  <xsl:template match="/EntityA/EntityB/@id">
    <xsl:attribute name="{name()}">foo2</xsl:attribute>
  </xsl:template>

  <xsl:template match="/EntityA/EntityB/@value">
    <xsl:attribute name="{name()}">bar2</xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

应用于旧的 XML:

<EntityA>
  <EntityB id="foo1" value="bar1" ignoredbutsave="bazz1"/>
</EntityA>

生成旧的 XML,并进行所需的属性值替换:

<EntityA>
  <EntityB id="foo2" value="bar2" ignoredbutsave="bazz1"/>
</EntityA>

关于xml - XSLT 更新仅给定属性的新值和 XPath 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19577698/

相关文章:

xml - XPath 版本(1.0、2.0、3.1)之间有什么区别

xml - xpath中有split函数吗?

xml - 用于修改 JetBrains 项目中 .idea 文件夹中 XML 的命令行工具

java - `@XmlRootElement` 和 `nillable`

xslt - 为孙子中的每个标记复制 node() 并用 XSLT 1.0 中的标记替换孙子的元素文本?

xml - JAXB 绑定(bind) : dynamic class names for repeated elements

android - 如何在android中将drawable设置为位图?

java - 应用 XSLT 时访问 w3.org 时出错

c# - 使用 .NET 基于属性在 XSLT 中选择元素

python - 使用selenium(python)抓取网页