Reputation: 1682
I need to match elements that have certain attributes but only if these attributes have the same name and value as some other attributes.
Here's the code:
<?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>
This @*[local-name() = $filterName]
matches the names of the attributes. Works fine. How can I do this for the values of the attributes so that I check the local-name() and the value at the same time?
EDIT:
<?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>
The attributes with which to match them:
<attlist>
<atts name="attr" value="premise"/>
<attsname="otherprops" value="html"/>
</attlist>
Expected output:
<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>
So basicallay: dont copy elements who have an attribute with the same name as the ones listed in attlist but have a different value (unless they have more values and at least one of them is listed in attlist). In this case, the elements that have otherprops="pdf" and atts="susu" are not copied.
Upvotes: 0
Views: 525
Reputation: 167571
You should have posted a sample of the two input documents. As far as I guess you simply want to replace <xsl:copy-of select="//some[@*[local-name() = $fName]] | //others[@*[local-name() = $fName]]"/>
with <xsl:copy-of select="//some[@*[local-name() = $fName and . = $fValue]] | //others[@*[local-name() = $fName and . = $fValue]]"/>
.
The cross-references are better done with a key but I would like a sample of the input to make suggestions on that.
Upvotes: 1