XSLT 1.0: copy everything except certain nodes according to value and variable
I have the following (simplified) XML that I get in a system environment:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<IS_LOG>
<USER>19291</USER>
<DATE>2011-08-15</DATE>
<TIME>15:36:36</TIME>
<SYST>sy1</SYST>
<MATERIALS>
<item>
<sy>100</sy>
<mat>000000000000310000</mat>
</item>
<item>
<sy>100</sy>
<mat>000000000000491078</mat>
</item>
</MATERIALS>
</IS_LOG>
</root>
The system that I work with passes me a variable at runtime that is not included in the XML structure above.
I have the following XSLT:
<?xml versio开发者_开发知识库n="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" version="1.0">
<!-- System variable whose value I normally only get only at runtime;
for test purposes set locally -->
<xsl:variable name="SenderService" select="'AT'"/>
<xsl:template match="@*|node()">
<xsl:choose>
<xsl:when test="$SenderService='AT'">
<xsl:copy>
<xsl:apply-templates mode="AT" select="@*|node()"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template mode="AT" match="item[mat > 000000000000299999 and mat < 000000000000399999]"/>
</xsl:stylesheet>
Now I need to copy all elements item
excluding the ones where mat
is in the number range of 300000 to 399999 and SenderService
is 'AT'.
If to test it locally I change the SenderService
in my XSLT to e.g. 'Z', the output looks fine, all items
get copied:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<IS_LOG>
<USER>19291</USER>
<DATE>2011-08-15</DATE>
<TIME>15:36:36</TIME>
<SYST>sy1</SYST>
<MATERIALS>
<item>
<sy>100</sy>
<mat>000000000000310000</mat>
</item>
<item>
<sy>100</sy>
<mat>000000000000491078</mat>
</item>
</MATERIALS>
</IS_LOG>
</root>
But if I set SenderService
to 'AT' the output looks like this:
<?xml version="1.0" encoding="UTF-8"?><root>
19291
2011-08-15
15:36:36
sy1
100
000000000000491078
</root>
The correct item gets copied but without the tags. How can I change the XSLT?
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="SenderService" select="'AT'"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="item[mat > 000000000000299999 and mat < 000000000000399999]">
<xsl:if test="$SenderService != 'AT'">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
精彩评论