我有一个源文件:

时间:2017-03-07 19:27:37

标签: xslt-2.0

I have a source document:

<?xml version = "1.0" encoding = "UTF-8"?>
<Circuit revision="B" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Block name="Design">
        <Data>
            <Layer name="INDEX_4" Function="PLANE"></Layer>
            <Layer name="INDEX_9" Function="CORE"></Layer>
            <Group name="PRIMARY">
                <SubGroup name="GROUP_PRIMARY">
                    <SubLayer Ref="INDEX_4" thickness="0.0350"></SubLayer>
                    <SubLayer Ref="INDEX_9" thickness="0.1000"></SubLayer>
                </SubGroup>
            </Group>
        </Data>
    </Block>
</Circuit>

使用current()的样式表提取相应的厚度:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
<xsl:template match="/Circuit/Block/Data/Layer">
    <xsl:element name="{name()}">
        <xsl:attribute name="id">
           <xsl:value-of select="@name"/>
        </xsl:attribute>
        <xsl:attribute name="Function">
          <xsl:value-of select="@Function"/>
        </xsl:attribute>
         <xsl:value-of select="/Circuit/Block/Data/Group/SubGroup/SubLayer[@Ref=current()/@name]/@thickness"/>
    </xsl:element>
</xsl:template>
 </xsl:stylesheet>

这是提取PREVIOUS厚度值的第二个样式表:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
<xsl:template match="/Circuit/Block/Data/Layer">
    <xsl:element name="{name()}">
        <xsl:attribute name="id">
           <xsl:value-of select="@name"/>
        </xsl:attribute>
        <xsl:attribute name="Function">
          <xsl:value-of select="@Function"/>
        </xsl:attribute>
         <xsl:value-of select="/Circuit/Block/Data/Group/SubGroup/SubLayer[@Ref=preceding-sibling::*[1]/@name]/@thickness"/>
    </xsl:element>
</xsl:template>
 </xsl:stylesheet>

在这两种情况下,我都明白了:

<?xml version="1.0" encoding="UTF-8"?>
    <Layer id="INDEX_4" Function="PLANE">0.0350</Layer>
    <Layer id="INDEX_9" Function="CORE">0.1000</Layer>

我尝试了前面的兄弟来获得前面的值 - 我想问题是&#34;前一个兄弟的上下文是什么?&#34;

是具有Layer元素的组还是具有SubLayer元素的组?

由于

拉尔夫B

1 个答案:

答案 0 :(得分:0)

在任何谓词中,上下文项是谓词前一步中的选定项,因此SubLayer[@Ref=preceding-sibling::*[1]/@name] @Refpreceding-sibling::*[1]/@name的上下文都是SubLayer元件。如果要选择相对于模板中当前匹配的Layer元素,则需要在谓词中使用current()/preceding-sibling::*[1]/@name