xml - XSL 在 for-each 中按属性查找元素

标签 xml templates xslt xpath

在我的 XML 文档中,我有以下类型的节点:

<parent>
  <value id="value1" name="Y" type="number"/>
  <value id="value2" name="X" type="number"/>
  <value id="value3" name="Z" type="operation" op="-" args="value1;value2"/>
</parent>

我想对其进行转换以获得完整的操作,如下所示:

<parent>
  <value id="value1" name="Y" type="number" />
  <value id="value2" name="X" type="number" />
  <operation>
    <name>Z = Y - X</name>
  </operation>
</parent>

我正在努力处理我的 xsl 模板。以下是完整的 XSL 代码:

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

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

    <xsl:template match="value[@type='operation']" priority="1">
        <xsl:variable name="name">
            <xsl:value-of select="concat(@name, ' = ')" />
            <xsl:for-each select="str:tokenize(@args, ';')">
                <xsl:choose>
                    <xsl:when test="//value[@id=current()]">
                        <xsl:value-of
                            select="concat(//value[@id=current()]/@name, ' ', @op, ' ')" />
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="concat(current(), ' ', @op, ' ')" />
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </xsl:variable>

        <operation>
            <name>
                <xsl:value-of select="$name" />
            </name>
        </operation>
    </xsl:template>

</xsl:stylesheet>

在 fore-each 内部,我检查它是否可以找到正确的节点,因为有时,操作的 @args 可能类似于 args="2.00;value1"

我的 for-each 内的测试显然有问题,因为我从上面显示的输入文件得到的结果是

<?xml version="1.0" encoding="UTF-8"?>
<parent>
    <value id="value1" name="Y" type="number" />
    <value id="value2" name="X" type="number" />
    <operation xmlns:str="http://exslt.org/strings">
        <name>Z = value1 value2  </name>
    </operation>
</parent>

应该进行什么测试才能获得正确值的名称?

最佳答案

通过 XSLT 1 和 EXSLT str:tokenize,您可以使用

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    exclude-result-prefixes="str"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://exslt.org/strings">

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

    <xsl:key name="val-ref" match="value[@id]" use="@id"/>

    <xsl:template match="value[@type='operation']" priority="1">
        <xsl:variable name="op" select="."/>
        <xsl:variable name="name">
            <xsl:value-of select="concat(@name, ' = ')" />
            <xsl:for-each select="str:tokenize(@args, ';')">
                <xsl:if test="position() > 1">
                    <xsl:value-of select="concat(' ', $op/@op, ' ')"/>
                </xsl:if>
                <xsl:variable name="value" select="."/>
                <xsl:for-each select="$op">
                    <xsl:choose>                  
                        <xsl:when test="key('val-ref', $value)">
                            <xsl:value-of
                                select="key('val-ref', $value)/@name" />
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="$value" />
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:for-each>
            </xsl:for-each>
        </xsl:variable>

        <operation>
            <name>
                <xsl:value-of select="$name" />
            </name>
        </operation>
    </xsl:template>

</xsl:stylesheet>

http://xsltransform.net/3MP2uCm

使用 XSLT 3,它变得更加紧凑:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:key name="val-ref" match="value[@id]" use="@id"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="value[@op]">
      <operation>
          <name>
             <xsl:value-of select="@name || ' = '"/>
             <xsl:value-of select="tokenize(@args, ';') ! (key('val-ref', ., current()/ancestor::parent)/@name, .)[1]" 
             separator=" {@op} "/>
          </name>
      </operation>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/jz1PuP6

关于xml - XSL 在 for-each 中按属性查找元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58427615/

相关文章:

xml - 如何根据 XSLT 1.0 中的一组值检查变量?

java - Saxon 9 HE、Java - 静态错误、XTSE0210、XTSE0165、XPST0017

xslt - <copy-of>和<apply-templates>有什么区别?

C++ 将模板参数包从一个可变参数模板传递到另一个模板会导致编译器错误

html - 在 XSL 文件的 <select> 语句中选择默认值

c# - 从 XElement 中读取元素与纯内容混合的数据

c# - 如何编写 Xelement 值?

xml - 如何使用 XSLT 获取基于条件匹配的位置?

.net - 在MVC3中如何将所有脚本移动到页面底部的</body>之前?

c++ - 对于构造函数,我如何在可变参数模板与 std::initializer_list 之间进行选择?