XSLT:使用变量存储和重用属性值

XSLT: Using Variable to store and reuse the Attribute Value

我正在尝试对 XML 文档进行更改,但在存储和重用属性值时遇到了问题。

该变量存储来自相同重复元素的所有值。但我希望它只存储和重用当前元素的值。

这是我的 XSLT:

    <!-- set them -->
    <xsl:variable name="Signatur" select=".//Field[@Type='2950']/@Value"/>
    <xsl:variable name="Datum" select=".//Field[@Type='9920']/@Value"/>

    <xsl:template match=".//Field[@Type='8450']/Field[@Type='8540']"/>

    <xsl:template match=".//Field[@Type='8450']">
        <h1:Field Type="8450" Value="digitale Reproduktion">
            <!-- use them -->
            <h1:Field Type="8540" Value="{$Signatur}"/>
            <h1:Field Type="8494" Value="{$Datum}"/>
            <xsl:apply-templates/>
        </h1:Field>
    </xsl:template>

the variables Signatur and Datum store both values from first and second document. But I want them to store and reuse just the values from current document.

看来您根本不想使用变量。为什么不直接对要输出的节点做一个相对引用呢?

<xsl:template match="Document/Field[@Type='8450']">
    <h1:Field Type="8450" Value="digitale Reproduktion">
        <h1:Field Type="8540" Value="{Field[@Type='8540']}"/>
        <h1:Field Type="8540" Value="{Field[@Type='8470']}"/>
        <xsl:apply-templates/>
    </h1:Field>
</xsl:template>

请注意,匹配表达式不必是与任何内容相关的完整 XPath 表达式。简单地说明模板应该匹配哪些节点,即而不是

<xsl:template match=".//Field[@Type='8450']">

使用

<xsl:template match="Field[@Type='8450']">

好的,一位同事帮助了我。前面的兄弟姐妹成功了:

 <xsl:template match=".//Field[@Type='8450']">
    <xsl:variable name="Signatur" select="preceding-sibling::Field[@Type='ob28']/Field[@Type='2950']/@Value"/>
    <xsl:variable name="Datum" select="preceding-sibling::Field[@Type='9920']/@Value"/>
    <h1:Field Type="8450" Value="digitale Reproduktion">
        <!-- use them -->
        <h1:Field Type="8540" Value="{$Signatur}"/>
        <h1:Field Type="8494" Value="{$Datum}"/>
        <xsl:apply-templates/>
    </h1:Field>
</xsl:template>