xml - 如何使用 Microsoft xslt 1.0 高效地稍微修改大型 xml 文档

标签 xml xslt-1.0

我想转换这个 xml:

<Root>
  <Result>
    <Message>
      <Header>
        <!-- Hundreds of child nodes -->        
      </Header>
      <Body>
        <Node1>
          <!-- Hundreds of child nodes -->
          <Node2>
            <!-- Hundreds of child nodes -->
            <Node3>
              <!-- Hundreds of child nodes -->
              <NodeX>value 1 to be changed</NodeX>
              <!-- Hundreds of child nodes -->
              <Node4>
                <!-- Hundreds of child nodes -->
                <NodeY>value 2 to be changed</NodeY>
              </Node4>
            </Node3>
          </Node2>          
        </Node1>        
      </Body>
      <RealValuesRoot>
        <!-- These two nodes -->
        <Value ID="1">this value must replace the value of Node X</Value>
        <Value ID="2">this value must replace the value of Node Y</Value>
      </RealValuesRoot>
    </Message>
  </Result>
  <!-- Hundreds to thousands of similar MessageRoot nodes -->
</Root>

进入这个 xml:

<Root>
  <Result>
    <Message>
      <Header>
        <!-- Hundreds of child nodes -->
      </Header>
      <Body>
        <Node1>
          <!-- Hundreds of child nodes -->
          <Node2>
            <!-- Hundreds of child nodes -->
            <Node3>
              <!-- Hundreds of child nodes -->
              <NodeX>this value must replace the value of Node X</NodeX>
              <!-- Hundreds of child nodes -->
              <Node4>
                <!-- Hundreds of child nodes -->
                <NodeY>this value must replace the value of Node Y</NodeY>
              </Node4>
            </Node3>
          </Node2>
        </Node1>
      </Body>
    </Message>
  </Result>
  <!-- Hundreds to thousands of similar MessageRoot nodes -->
</Root>

除了以下变化外,输出与输入几乎相同:

  1. X 和 Y 节点值必须替换为/RealValuesRoot/Value 节点的值。
  2. 必须从输出中删除/RealValuesRoot 节点。
  3. xml 的其余部分必须在输出中保持不变。

“值”节点具有唯一 ID,表示消息正文中的唯一 xpath,例如ID 1 refres to xpath/Message/Body/Node1/Node2/Node3/NodeX.

我必须使用微软的 xslt 1.0 版!!

我已经有一个 xslt 可以正常工作并且可以做我想做的一切,但我对性能不满意!

我的 xslt 工作方式如下:

  1. 我创建了一个类似于键值对的全局字符串变量,类似于:1:xpath1_2:xpath2_ … _N:xpathN。此变量将“值”节点的 ID 与消息正文中需要替换的节点相关联。

  2. xslt 从根节点开始递归迭代输入的 xml。

  3. 我计算当前节点的 xpath,然后执行以下操作之一:

    1. 如果当前 xpath 与全局列表中的 xpath 之一完全匹配,那么我将其值替换为相应“Value”节点的值并继续迭代。
    2. 如果当前 xpath 引用“RealValuesRoot”节点,那么我将忽略该节点(不要将其复制到输出)并继续递归迭代。
    3. 如果全局 ID-xpath 字符串中不存在当前 xpath,那么我将完整节点复制到输出并继续迭代。 (这发生在例如/Message/Header 节点中,它永远不会包含任何需要替换的节点)
    4. 如果当前 xpath 部分匹配全局列表中的 xpath 之一,那么我将继续递归迭代,直到达到上述 3 种情况之一。

如前所述,我的 xslt 工作正常,但我想尽可能提高性能,请随时提出一个完整的新 xslt 逻辑!欢迎提出您的想法和建议!

最佳答案

I compute the xpath for the current node then do one of the following...

这可能是您的效率低下 - 如果您每次都重新计算返回根的路径,您可能正在查看 O(N2) 算法。没有看到您的 XSLT,这是相当推测性的,但您可以通过使用参数将当前路径传递给递归来稍微调整一下 - 如果您的主要算法基于标准身份模板

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

然后把它改成类似的东西

<xsl:template match="@*|node()">
  <xsl:param name="curPath" />
  <xsl:copy>
    <xsl:apply-templates select="@*|node()">
      <xsl:with-param name="curPath" select="concat($curPath, '/', name())" />
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

或者您构建所需路径的任何逻辑。现在,在您要处理的节点的特定模板中,您已经有了到它们父节点的路径,并且您不必每次都一直走到根节点。

您可能需要添加一个

<xsl:template match="/">
  <xsl:apply-templates />
</xsl:template>

所以你不会在 $curPath 的前面得到双斜线


The xpaths are hardcoded in the xslt as a string in a global variable

如果您在 XML 结构中表示此映射而不是字符串,那么您可以使用 key 机制来加快查找速度:

<xsl:variable name="rtfLookupTable">
  <lookuptable>
    <val xpath="/first/xpath/expression" id="1" />
    <val xpath="/second/xpath/expression" id="2" />
    <!-- ... -->
  </lookuptable>
</xsl:variable>

<xsl:variable name="lookupTable" select="msxsl:node-set($rtfLookupTable)" />

<xsl:key name="valByXpath" match="val" use="@xpath" />

(将 xmlns:msxsl="urn:schemas-microsoft-com:xslt" 添加到您的 xsl:stylesheet)。或者,如果您想避免使用 node-set 扩展函数,那么另一种定义可能是

<xsl:variable name="lookupTable" select="document('')//xsl:variable[name='rtfLookupTable']" />

其工作原理是将样式表本身视为纯 XML 文档。

跨多个文档的键在 XSLT 1.0 中有点繁琐,但可以做到,本质上你必须在调用键函数之前切换当前上下文以指向 $lookupTable,所以你需要将当前上下文保存在变量中以供您稍后引用:

<xsl:template match="text()">
  <xsl:param name="curPath" />

  <xsl:variable name="dot" select="." />
  <xsl:variable name="slash" select="/" />

  <xsl:for-each select="$lookupTable">
    <xsl:variable name="valId" select="key('valByXpath', $curPath)/@id" />
    <xsl:choose>
      <xsl:when test="$valId">
        <xsl:value-of select="$slash//Value[@id = $valId]" />
        <!-- or however you extract the right Value -->
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$dot" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each>
</xsl:template>

或者事实上,为什么不让 XSLT 引擎为您完成这些繁重的工作。而不是将您的映射表示为字符串

/path/to/node1_1:/path/to/node2_2

直接将其表示为模板

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <!-- copy everything as-is apart from exceptions below -->
  <xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
  </xsl:template>

  <!-- delete the RealValuesRoot -->
  <xsl:template match="RealValuesRoot" />

  <xsl:template match="/path/to/node1">
    <xsl:copy><xsl:value-of select="//Value[id='1']" /></xsl:copy>
  </xsl:template>
  <xsl:template match="/path/to/node2">
    <xsl:copy><xsl:value-of select="//Value[id='2']" /></xsl:copy>
  </xsl:template>
</xsl:stylesheet>

我相信您可以看到如何使用某种模板机制(甚至可以是另一种 XSLT)从现有映射轻松自动生成特定模板。

关于xml - 如何使用 Microsoft xslt 1.0 高效地稍微修改大型 xml 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19277390/

相关文章:

android - 更改 Android 的 xml 中的菜单导航图标颜色

c# - 序列化为 XML 时如何处理 JSON 键中的空格?

c - 如何在 libxml2 中添加由字符串构造的 xml 节点

xml - XSLT 迭代子节点

xpath - XSLT 三元 "If"运算符?

xml - 将元素与 xsi :type attribute 匹配

python - 使用python etree提取xml文件的一部分

java - 动态继承命名空间的 XSL 元素

msxml - 如何使用将在 MSXML 和 libxml 上运行的 node-set() 函数编写 XSL 1.0 样式表

xml - 错误 : 'Unsupported XSL element ' http://www. w3.org/1999/XSL/Transform:for-each-group''