How to check tags in lookup table
I have, this xml lookup table:
<lookup>
<Codes>
<code>123<code>
</Codes>
<Codes>
<code>321<code>
</Codes>
</lookup>
and document:
<document>
<header>
<remarks>test</remarks>
</header>
<Line>
<Line-Item>
<code>123</code>
</Line-Item>
<Line-Item>
<code>444</code>
</Line-Item>
<Line-Item>
<code>321</c开发者_如何转开发ode>
</Line-Item>
</Line>
</document>
There is possible, to check document codes with lookup table codes, and if document code=lookup code, delete Line-Item from document.
answer should be:
<document>
<header>
<remarks>test</remarks>
</header>
<Line>
<Line-Item>
<code>444</code>
</Line-Item>
</Line>
</document>
I'm stuck on xsl:stylesheet version="1.0". Best regards :)
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vCodes" select=
"document('file:///c:/temp/LookupDelete.xml')/*/*/code"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"Line-Item[code
= document('file:///c:/temp/LookupDelete.xml')
/*/*/code
]
"/>
<xsl:template match=
"Line[not(Line-Item/code
[not(. = document('file:///c:/temp/LookupDelete.xml')
/*/*/code
)
]
)
]
"/>
</xsl:stylesheet>
when applied on the provided XML document:
<document>
<header>
<remarks>test</remarks>
</header>
<Line>
<Line-Item>
<code>123</code>
</Line-Item>
<Line-Item>
<code>444</code>
</Line-Item>
<Line-Item>
<code>321</code>
</Line-Item>
</Line>
</document>
produces the wanted, correct result:
<document>
<header>
<remarks>test</remarks>
</header>
<Line>
<Line-Item>
<code>444</code>
</Line-Item>
</Line>
</document>
If the document is this:
<document>
<header>
<remarks>test</remarks>
</header>
<Line>
<Line-Item>
<code>123</code>
</Line-Item>
<Line-Item>
<code>321</code>
</Line-Item>
<Line-Item>
<code>321</code>
</Line-Item>
</Line>
</document>
then again the correct result is produced (note that no Line
element is output at all):
<document>
<header>
<remarks>test</remarks>
</header>
</document>
<!-- codes of items to exclude from external document -->
<xsl:variable name="lookup-table" select="document('lookup-table.xml')/lookup/Codes/code"/>
<!-- exclude Line-Items with codes from lookup table -->
<xsl:template match="Line-Item[ code = $lookup-table ]"/>
<!--
exclude <Line> elements,
which contains only <Line-Item>'s which will be removed
-->
<xsl:template match="Line[ not(Line-Item[ code != $lookup-table ]) ]"/>
<!-- copy all other items -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl;apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
This stylesheet produces the desired result:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:lookup="lookup">
<lookup:lookup>
<Codes>
<code>123</code>
</Codes>
<Codes>
<code>321</code>
</Codes>
</lookup:lookup>
<xsl:variable name="lookup" select="document('')/*/*/Codes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Line">
<xsl:copy>
<xsl:apply-templates select="@* |
node()[not(self::Line-Item[code=$lookup/code])]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Note that the lookup table is included in the stylesheet. It could easily be included in a separate document if it's too large to include in the transformation.
精彩评论