xslt 2.0检查元素的所有属性的名称和值

时间:2015-05-18 20:29:55

标签: xml xslt

我需要匹配具有某些属性的元素,但前提是这些属性与其他属性具有相同的名称和值。

以下是代码:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="fn xs fo">

    <xsl:output method="xml" standalone="yes" version="1.0" encoding="utf-8"/>

    <xsl:strip-space elements="*"/>

    <xsl:param name="attDoc" select="doc(iri-to-uri('atts_doc.xml'))"/>

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

    <xsl:template match="the_elem">
        <xsl:variable name="curID" select="@id"/>
        <xsl:variable name="fName" select="$attDoc//elem[@id = $curID]//atts/@name"/>
        <xsl:variable name="fValue" select="$attDoc//elem[@id = $curID]//atts/@value"/>

        <xsl:copy-of select="//some[@*[local-name() = $fName]] | //others[@*[local-name() = $fName]]"/>

        </xsl:template>

</xsl:stylesheet>

@*[local-name() = $filterName]匹配属性的名称。工作良好。如何为属性值执行此操作,以便同时检查local-name()和值?

修改

<?xml version="1.0" encoding="UTF-8"?>
<doc>
    <the_elem id="e7de53a557c6409abf562db7af99a89b" konkreterTyp="buildable-doc"  InDB="d001400051967574.ditadoc" version="1">
        <docref id="9df14752de4f43bfa523da1e152ce08c"  InDB="d031400072009034.xml" href="9df14752de4f43bfa523da1e152ce08c.xml" otherprops="html" version="1"/>
        <subdoc id="ecf8fa0cc3fb4b0e9f1683b1ac152470">
            <docref id="19a0c005d6cd44b79be9f45ca03f5b09"  InDB="d031399034707858.xml" href="19a0c005d6cd44b79be9f45ca03f5b09.xml" version="1" attr="premise"/>
            <docref id="0455ad9195c0445fa27d9d8f07b28104"  InDB="d031399035470408.xml" href="0455ad9195c0445fa27d9d8f07b28104.xml" version="1" attr="susu"/>
                <docref id="537ecda2f8144fe3b001dbb6edf9911b"  InDB="d031399556993345.xml" href="537ecda2f8144fe3b001dbb6edf9911b.xml" version="1" attr="premise"/>
                <docref id="955db10971fa40c19962fa426f438642"  InDB="d031399557945583.xml" href="955db10971fa40c19962fa426f438642.xml" version="1"/>
                <docref id="041a4e15910b4440b157dc5a3f71e5a7"  InDB="d031399553954140.xml" href="041a4e15910b4440b157dc5a3f71e5a7.xml" version="1"/>
            </docref>
            <docref id="f7e7fc573a804b2dbf1a29ac34d77a38"  InDB="d031399045294650.xml" href="f7e7fc573a804b2dbf1a29ac34d77a38.xml" version="1"/>
            <docref id="bc838c1aed1c45a7adef21df0fc9bb78"  InDB="d031399378748218.xml" href="bc838c1aed1c45a7adef21df0fc9bb78.xml" version="1" attr="premise tftft"/>
            <docref id="ae88f1d7ee71443b958aa5b961ae5acb"  InDB="d031399036493818.xml" href="ae88f1d7ee71443b958aa5b961ae5acb.xml" version="1" otherprops="html">
                <docref id="95c3677a0c6148a7916e8e7ae3244a49"  InDB="d031399037348724.xml" href="95c3677a0c6148a7916e8e7ae3244a49.xml" version="1" otherprops="html"/>
                <docref id="6d0cbe6217ba427f86355078a5584ae1"  InDB="d031399039534037.xml" href="6d0cbe6217ba427f86355078a5584ae1.xml" version="1"/>
                <docref id="75fc98967b1346e59170f3f4ffb2c74d"  InDB="d031399040481501.xml" href="75fc98967b1346e59170f3f4ffb2c74d.xml" version="1" otherprops="html pdf"/>
                <docref id="02bde441fe584af2a94219966f7d1b7b"  InDB="d031399040743199.xml" href="02bde441fe584af2a94219966f7d1b7b.xml" version="1"/>
                <docref id="a5c13a2c2e5a4e97ad6dbc6a92329b0c"  InDB="d031399041552299.xml" href="a5c13a2c2e5a4e97ad6dbc6a92329b0c.xml" version="1" otherprops="html"/>
                <docref id="713d6a7c7d7f4b6f850c59296caec8d6"  InDB="d031399041720838.xml" href="713d6a7c7d7f4b6f850c59296caec8d6.xml" version="1"/>
            </docref>
            <docref id="fb70161b2b644329a5873117673a8866"  InDB="d031399042188509.xml" href="fb70161b2b644329a5873117673a8866.xml" version="1">
                <docref id="762632eb8ea148599cec1fc95dec5363"  InDB="d031399042339264.xml" href="762632eb8ea148599cec1fc95dec5363.xml" version="1" otherprops="pdf"/>
                <docref id="e68e6a719fad482e9f4a0d9669395b7b"  InDB="d031399043459705.xml" href="e68e6a719fad482e9f4a0d9669395b7b.xml" version="1"/>
                <docref id="752fc6f1607249c88178e85d30bc240b"  InDB="d031399043986595.xml" href="752fc6f1607249c88178e85d30bc240b.xml" version="1"/>
            </docref>
            <docref id="b404a621246f4875899e9eaa7bbd6083"  InDB="d031399370347896.xml" href="b404a621246f4875899e9eaa7bbd6083.xml" version="1"/>
        </subdoc>
        <docref id="d5efd81f13d94525892544a1a962361f"  InDB="d041398160534858.xml" href="d5efd81f13d94525892544a1a962361f.xml" print="no" version="1" otherprops="pdf"/>
    </the_elem>
</doc>

与之匹配的属性:

        <attlist>
            <atts name="attr" value="premise"/>
            <attsname="otherprops" value="html"/>
</attlist>

预期产出:

<doc>
    <the_elem id="e7de53a557c6409abf562db7af99a89b" konkreterTyp="buildable-doc"  InDB="d001400051967574.ditadoc" version="1">
        <docref id="9df14752de4f43bfa523da1e152ce08c"  InDB="d031400072009034.xml" href="9df14752de4f43bfa523da1e152ce08c.xml" otherprops="html" version="1"/>
        <subdoc id="ecf8fa0cc3fb4b0e9f1683b1ac152470">
            <docref id="19a0c005d6cd44b79be9f45ca03f5b09"  InDB="d031399034707858.xml" href="19a0c005d6cd44b79be9f45ca03f5b09.xml" version="1" attr="premise"/>
                <docref id="537ecda2f8144fe3b001dbb6edf9911b"  InDB="d031399556993345.xml" href="537ecda2f8144fe3b001dbb6edf9911b.xml" version="1" attr="premise"/>
                <docref id="955db10971fa40c19962fa426f438642"  InDB="d031399557945583.xml" href="955db10971fa40c19962fa426f438642.xml" version="1"/>
                <docref id="041a4e15910b4440b157dc5a3f71e5a7"  InDB="d031399553954140.xml" href="041a4e15910b4440b157dc5a3f71e5a7.xml" version="1"/>
            </docref>
            <docref id="f7e7fc573a804b2dbf1a29ac34d77a38"  InDB="d031399045294650.xml" href="f7e7fc573a804b2dbf1a29ac34d77a38.xml" version="1"/>
            <docref id="bc838c1aed1c45a7adef21df0fc9bb78"  InDB="d031399378748218.xml" href="bc838c1aed1c45a7adef21df0fc9bb78.xml" version="1" attr="premise tftft"/>
            <docref id="ae88f1d7ee71443b958aa5b961ae5acb"  InDB="d031399036493818.xml" href="ae88f1d7ee71443b958aa5b961ae5acb.xml" version="1" otherprops="html">
                <docref id="95c3677a0c6148a7916e8e7ae3244a49"  InDB="d031399037348724.xml" href="95c3677a0c6148a7916e8e7ae3244a49.xml" version="1" otherprops="html"/>
                <docref id="6d0cbe6217ba427f86355078a5584ae1"  InDB="d031399039534037.xml" href="6d0cbe6217ba427f86355078a5584ae1.xml" version="1"/>
                <docref id="75fc98967b1346e59170f3f4ffb2c74d"  InDB="d031399040481501.xml" href="75fc98967b1346e59170f3f4ffb2c74d.xml" version="1" otherprops="html pdf"/>
                <docref id="02bde441fe584af2a94219966f7d1b7b"  InDB="d031399040743199.xml" href="02bde441fe584af2a94219966f7d1b7b.xml" version="1"/>
                <docref id="a5c13a2c2e5a4e97ad6dbc6a92329b0c"  InDB="d031399041552299.xml" href="a5c13a2c2e5a4e97ad6dbc6a92329b0c.xml" version="1" otherprops="html"/>
                <docref id="713d6a7c7d7f4b6f850c59296caec8d6"  InDB="d031399041720838.xml" href="713d6a7c7d7f4b6f850c59296caec8d6.xml" version="1"/>
            </docref>
            <docref id="fb70161b2b644329a5873117673a8866"  InDB="d031399042188509.xml" href="fb70161b2b644329a5873117673a8866.xml" version="1">
                <docref id="e68e6a719fad482e9f4a0d9669395b7b"  InDB="d031399043459705.xml" href="e68e6a719fad482e9f4a0d9669395b7b.xml" version="1"/>
                <docref id="752fc6f1607249c88178e85d30bc240b"  InDB="d031399043986595.xml" href="752fc6f1607249c88178e85d30bc240b.xml" version="1"/>
            </docref>
            <docref id="b404a621246f4875899e9eaa7bbd6083"  InDB="d031399370347896.xml" href="b404a621246f4875899e9eaa7bbd6083.xml" version="1"/>
        </subdoc>
    </the_elem>
</doc>

所以basicallay:不要复制具有与attlist中列出的属性相同的属性但具有不同值的元素(除非它们具有更多值,并且至少其中一个列在attlist中)。在这种情况下,不复制具有otherprops =“pdf”和atts =“susu”的元素。

1 个答案:

答案 0 :(得分:1)

您应该已经发布了两个输入文档的示例。据我猜您只想将<xsl:copy-of select="//some[@*[local-name() = $fName]] | //others[@*[local-name() = $fName]]"/>替换为<xsl:copy-of select="//some[@*[local-name() = $fName and . = $fValue]] | //others[@*[local-name() = $fName and . = $fValue]]"/>

使用密钥可以更好地完成交叉引用,但我想要一个输入样本来对此提出建议。