XSL模板匹配基于另一个元素的属性值

时间:2013-05-07 17:25:37

标签: xml xslt

我已成功使用XSL查找并替换XML标记,但发现我的XSL可能无法正常工作。

源XML:

<article>
    <parastyles>
        <parastyle name="Headline Bold" uid="876"/>
        <parastyle name="Head babies-recipe" uid="877"/>
        <parastyle name="Byline Paper" uid="885"/>
        <parastyle name="Byline Name" uid="886"/>
        <parastyle name="Body Copy" uid="904"/>
    </parastyles>
    <charstyles>
        <charstyle name="[None]" uid="103"/>
    </charstyles>
    <story name="body">
        <runs>
            <run p="886" c="103">By AUTHOR NAME
                <eol/>
            </run>
                <run p="885" c="103">Local Writer
                <eol/>
            </run>
            <run p="904" c="103">CITY — Borough Police en
                <eol hyphenated="true"/>
                countered three men in separate cases
                <eol/>
                recently who all claimed they had for
                <eol hyphenated="true"/>
                gotten they were carrying drugs or drug
                <eol/>
                paraphernalia until an officer started
                <eol/>
                asking questions.
            </run>
            <run p="877" c="103">
                Forgot joints
                <eol/>
            </run>
            <run p="904" c="103">
                In another case, City
                <eol/>
                Police were called to a home
                <eol/>
                on LaSalle Street March 30
                <eol/>
                for a report of a man banging
                <eol/>
                on the door.
                <eol/>
            </run>
            <run p="877" c="103">
                Forgot pipe
                <eol/>
            </run>
            <run p="904" c="103">
                In a third case, Ptlm. Raf
                <eol hyphenated="true"/>
                ferty spotted a young man
                <eol/>
                running along West Second
                <eol/>
                Street and ducking into an
                <eol/>
                area near the Salvation Army
                <eol/>
                drop-off on March 28 around
                <eol/>
                1:40 a.m.
                <eol/>
            </run>
        </runs>
    </story>
</article>

我的XSL如下:

<xsl:template match="run[@p='877']">
    <xsl:text>&lt;strong&gt;</xsl:text><xsl:value-of select="."/><xsl:text>&lt;/strong&gt;</xsl:text>
</xsl:template>

这会使所选行包含所需的标记。但是,我真正需要的是用一个名为“Head baby-recipe”的行包装。到目前为止,他们都有一个“877”的uid并且使用run [@ p ='877']已经有效了。然而,可能有一个例子是uid不是“877”。所以需要我的'匹配'语句来寻找一个p等于parastyle的uid,其名称是“Head baby-recipe”。这有点复杂,我无法为此开发XSL。

感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

它应该是那样的(对于“Byline Name”):

run[@p=/article/parastyles/parastyle[@name='Byline Name']/@uid]