首页 > 解决方案 > 如何使用评估

问题描述

我目前正在查看我们有一个样式表要处理的 XML 代码。

我的代码如下所示

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-model href="register_tool/rules/rules.sch" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"?>

<addrmap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="register_tool/rules/man_schema.xsd" name="di_do_1430_board" offset="0x0" lsb_size="32">

    <enum name="ch_handler_ch_type" bitfield="false">
        <element name="be" value="0" offset="0x00000000"/> 
    </enum>

    <addrmap name="be_sys" offset="0x00000000" offset_xpath="addrmap/enum/element[@name='be']/@offset" lsb_size="24">
        <ref name="be_reg_acc" path="blocks/be_reg_acc.xml" ref_name="be_reg_acc" offset="0x0" />
    </addrmap>

</addrmap>

我的样式表的一部分看起来如下

<xsl:choose>
  <xsl:when test="@offset_xpath">
    <xsl:variable name="resolved_offset"><xsl:evaluate xpath="@offset_xpath"></xsl:evaluate></xsl:variable>
    <xsl:apply-templates mode="map">
      <xsl:with-param name="offset" select="rdt:all2dec($resolved_offset)"/>
    </xsl:apply-templates>
  </xsl:when>
  <xsl:otherwise>
    <xsl:apply-templates mode="map">
      <xsl:with-param name="offset" select="rdt:all2dec(@offset)"/>
    </xsl:apply-templates>
  </xsl:otherwise> 
</xsl:choose>

我试图从我的 enum(ch_handler_ch_type) 中获取值,但使用上面的表达式 xsl:evaluate 只给了我#document/fragment 我无法弄清楚如何获取特定值

我正在使用 Saxon-EE 9.8.0.8

问候

编辑

我已将 xi:include 替换为枚举,以便更清楚地了解文件的外观。我的目标是 offset_xpath 所在的子addrmap节点,我需要更改偏移值。使用枚举节点中定义的偏移值。

我已经成功地通过使用从枚举中获取值

<xsl:variable name="resolved_offset"><xsl:value-of><xsl:evaluate xpath="@offset_xpath" context-item="." as="xs:string"></xsl:evaluate></xsl:value-of></xsl:variable>

现在我只是不知道如何更改节点 addrmap 中的属性值偏移量。

我试着做一个模板

<xsl:template match="@offset[parent::addrmap]">
  <xsl:param name="resolved_offset" tunnel="yes"/>
  <xsl:attribute name="offset">
    <xsl:value-of select="$resolved_offset"/>
  </xsl:attribute>
</xsl:template>

我还可以看到它被正确调用但是没有发生变化

标签: xmlsaxonxslt-3.0

解决方案


通常,有必要定义context-itemfor xsl:evaluate,如果我们假设您xsl:evaluate在内部的上下文中具有 the ,addrmap那么您拥有的路径addrmap/enum/element[@name='be']/@offset只有在您想相对于根节点评估它时才有意义,/因此您需要例如

<xsl:evaluate xpath="@offset_xpath" context-item="/" as="xs:string"/>

或者您需要更改路径。我添加了as="xs:string"作为选择单个属性节点而不是它的值会导致您的xsl:value-of.

这是一个更完整的示例xsl:evaluate

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    exclude-result-prefixes="xs math"
    version="3.0">

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

    <xsl:template match="enum"/>

    <xsl:template match="addrmap[@offset_xpath]/ref/@offset">
        <xsl:attribute name="{name()}">
            <xsl:evaluate xpath="../../@offset_xpath" context-item="/"/>
        </xsl:attribute>
    </xsl:template>

</xsl:stylesheet>

它将您最新的输入样本转换为

<addrmap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="di_do_1430_board" offset="0x0" lsb_size="32">



    <addrmap name="be_sys" offset="0x00000000" offset_xpath="addrmap/enum/element[@name='be']/@offset" lsb_size="24">
        <ref name="be_reg_acc" path="blocks/be_reg_acc.xml" ref_name="be_reg_acc" offset="0x00000000"/>
    </addrmap>

</addrmap>

如您所见,元素offsetrefaddrmap元素的属性已更改。

仍然无法理解您对给定路径的尝试,context-item="."因为我不明白为什么相对路径addrmap/enum/element[@name='be']/@offset会选择任何东西。

在前面的示例中,不清楚需要做什么或需要做什么,一般来说,我认为通过简单地编写匹配模式来检查某个节点结构的rdt:all2dec尝试更容易处理,就像我已经完成的那样,对于另一种情况,你可以使用例如。xsl:choose/xsl:whenmatch="addrmap[@offset_xpath]/ref/@offset<xsl:template match="addrmap[not(@offset_xpath)]/ref/@offset">


推荐阅读