Xml 每种类型的XSL组合不同的运算符
我有这个XML文件,我需要用三种方式过滤它: 1) 颜色应该是红色或绿色,所有其他颜色都应该过滤掉 2) 类别可以是除catB之外的任何类别 3) 状态可以是任何状态,但丢失或损坏除外Xml 每种类型的XSL组合不同的运算符,xml,xslt,foreach,operators,Xml,Xslt,Foreach,Operators,我有这个XML文件,我需要用三种方式过滤它: 1) 颜色应该是红色或绿色,所有其他颜色都应该过滤掉 2) 类别可以是除catB之外的任何类别 3) 状态可以是任何状态,但丢失或损坏除外 <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="items.xsl"?> <list> <item> <description>Green cat
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="items.xsl"?>
<list>
<item>
<description>Green catA Damaged</description>
<include>N</include>
<colour>GREEN</colour>
<category>catA</category>
<status>DAMAGED</status>
</item>
<item>
<description>Red catA OK</description>
<include>Y</include>
<colour>RED</colour>
<category>catA</category>
<status>OK</status>
</item>
<item>
<description>Green catB OK</description>
<include>N</include>
<colour>GREEN</colour>
<category>catB</category>
<status>OK</status>
</item>
<item>
<description>Red catB OK</description>
<include>N</include>
<colour>RED</colour>
<category>catB</category>
<status>OK</status>
</item>
<item>
<description>Blue catB OK</description>
<include>N</include>
<colour>BLUE</colour>
<category>catC</category>
<status>OK</status>
</item>
<item>
<description>Yellow catC OK</description>
<include>N</include>
<colour>YELLOW</colour>
<category>catC</category>
<status>OK</status>
</item>
<item>
<description>Green catA OK</description>
<include>Y</include>
<colour>GREEN</colour>
<category>catA</category>
<status>OK</status>
</item>
<item>
<description>Green catB Missing</description>
<include>N</include>
<colour>GREEN</colour>
<category>catB</category>
<status>MISSING</status>
</item>
<item>
<description>Red catB Missing</description>
<include>N</include>
<colour>RED</colour>
<category>catA</category>
<status>MISSING</status>
</item>
<item>
<description>Red catC Damaged</description>
<include>N</include>
<colour>RED</colour>
<category>catC</category>
<status>DAMAGED</status>
</item>
</list>
绿卡塔受损
N
绿色
卡塔
损坏
红色卡塔OK
Y
红色
卡塔
好啊
绿色catB OK
N
绿色
有线电视
好啊
红色catB OK
N
红色
有线电视
好啊
蓝色catB OK
N
蓝色
catC
好啊
黄色catC OK
N
黄色的
catC
好啊
绿色卡塔OK
Y
绿色
卡塔
好啊
绿色有线电视失踪
N
绿色
有线电视
丢失的
红色有线电视失踪
N
红色
卡塔
丢失的
红色catC损坏
N
红色
catC
损坏
我试着在第一个(积极的)条件下各选一个,在另外两个(消极的)条件下各选一个——但我到底应该如何将它们组合成一个呢?或者,是否有其他更好的替代方法来代替每种方法。
谷歌搜索和检查三本书都没有给我答案,所以我有一种奇怪的感觉,我完全错了
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<list>
<!-- "positive" statement
<xsl:for-each select="list/item[colour='GREEN' or colour='RED']">
"negative" statement
<xsl:for-each select="list/item[status!='MISSING'][status!='DAMAGED'][category!='catB']">
-->
<xsl:sort select="description"/>
<itemline>
<description><xsl:value-of select="description"/></description>
<include><xsl:value-of select="include"/></include>
<colour><xsl:value-of select="colour"/></colour>
<category><xsl:value-of select="category"/></category>
<status><xsl:value-of select="status"/></status>
</itemline>
</xsl:for-each>
</list>
</xsl:template>
</xsl:stylesheet>
1) 颜色应为红色或绿色,所有其他颜色应经过过滤
远离
2) 类别可以是除catB之外的任何类别
3) 状态可以是任何状态,但丢失或损坏除外
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="items.xsl"?>
<list>
<item>
<description>Green catA Damaged</description>
<include>N</include>
<colour>GREEN</colour>
<category>catA</category>
<status>DAMAGED</status>
</item>
<item>
<description>Red catA OK</description>
<include>Y</include>
<colour>RED</colour>
<category>catA</category>
<status>OK</status>
</item>
<item>
<description>Green catB OK</description>
<include>N</include>
<colour>GREEN</colour>
<category>catB</category>
<status>OK</status>
</item>
<item>
<description>Red catB OK</description>
<include>N</include>
<colour>RED</colour>
<category>catB</category>
<status>OK</status>
</item>
<item>
<description>Blue catB OK</description>
<include>N</include>
<colour>BLUE</colour>
<category>catC</category>
<status>OK</status>
</item>
<item>
<description>Yellow catC OK</description>
<include>N</include>
<colour>YELLOW</colour>
<category>catC</category>
<status>OK</status>
</item>
<item>
<description>Green catA OK</description>
<include>Y</include>
<colour>GREEN</colour>
<category>catA</category>
<status>OK</status>
</item>
<item>
<description>Green catB Missing</description>
<include>N</include>
<colour>GREEN</colour>
<category>catB</category>
<status>MISSING</status>
</item>
<item>
<description>Red catB Missing</description>
<include>N</include>
<colour>RED</colour>
<category>catA</category>
<status>MISSING</status>
</item>
<item>
<description>Red catC Damaged</description>
<include>N</include>
<colour>RED</colour>
<category>catC</category>
<status>DAMAGED</status>
</item>
</list>
那么:
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/list">
<list>
<xsl:for-each select="item
[colour='RED' or colour='GREEN']
[not(category='catB')]
[not(status='MISSING' or status='DAMAGED')]">
<xsl:sort select="description"/>
<itemline>
<xsl:copy-of select="*"/>
</itemline>
</xsl:for-each>
</list>
</xsl:template>
</xsl:stylesheet>
我有这个XML文件,我需要用三种方式过滤它:
1) 颜色应为红色或绿色,所有其他颜色应经过过滤
远离
2) 类别可以是除catB之外的任何类别
3) 状态可以是任何状态,但丢失或损坏除外
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="items.xsl"?>
<list>
<item>
<description>Green catA Damaged</description>
<include>N</include>
<colour>GREEN</colour>
<category>catA</category>
<status>DAMAGED</status>
</item>
<item>
<description>Red catA OK</description>
<include>Y</include>
<colour>RED</colour>
<category>catA</category>
<status>OK</status>
</item>
<item>
<description>Green catB OK</description>
<include>N</include>
<colour>GREEN</colour>
<category>catB</category>
<status>OK</status>
</item>
<item>
<description>Red catB OK</description>
<include>N</include>
<colour>RED</colour>
<category>catB</category>
<status>OK</status>
</item>
<item>
<description>Blue catB OK</description>
<include>N</include>
<colour>BLUE</colour>
<category>catC</category>
<status>OK</status>
</item>
<item>
<description>Yellow catC OK</description>
<include>N</include>
<colour>YELLOW</colour>
<category>catC</category>
<status>OK</status>
</item>
<item>
<description>Green catA OK</description>
<include>Y</include>
<colour>GREEN</colour>
<category>catA</category>
<status>OK</status>
</item>
<item>
<description>Green catB Missing</description>
<include>N</include>
<colour>GREEN</colour>
<category>catB</category>
<status>MISSING</status>
</item>
<item>
<description>Red catB Missing</description>
<include>N</include>
<colour>RED</colour>
<category>catA</category>
<status>MISSING</status>
</item>
<item>
<description>Red catC Damaged</description>
<include>N</include>
<colour>RED</colour>
<category>catC</category>
<status>DAMAGED</status>
</item>
</list>
我会这样写:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<xsl:apply-templates select=
"item[colour[. = 'RED' or . = 'GREEN']
and not(category='catB' or status[. = 'MISSING' or . = 'DAMAGED'])]">
<xsl:sort select="description"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="item">
<itemline><xsl:copy-of select="*"/></itemline>
</xsl:template>
</xsl:stylesheet>
<itemline>
<description>Green catA OK</description>
<include>Y</include>
<colour>GREEN</colour>
<category>catA</category>
<status>OK</status>
</itemline>
<itemline>
<description>Red catA OK</description>
<include>Y</include>
<colour>RED</colour>
<category>catA</category>
<status>OK</status>
</itemline>
not(contains($myValues, concat('|',myElem, '|')))
可以这样写:
contains('|value1|value2|value3|value4|value5|', concat('|',myElem, '|'))
或者,如果第一个参数字符串是变量的值$myValues
:
contains($myValues, concat('|',myElem, '|'))
类似地,myElem
值的快捷方式不是众多值中的一个:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<xsl:apply-templates select=
"item[colour[. = 'RED' or . = 'GREEN']
and not(category='catB' or status[. = 'MISSING' or . = 'DAMAGED'])]">
<xsl:sort select="description"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="item">
<itemline><xsl:copy-of select="*"/></itemline>
</xsl:template>
</xsl:stylesheet>
<itemline>
<description>Green catA OK</description>
<include>Y</include>
<colour>GREEN</colour>
<category>catA</category>
<status>OK</status>
</itemline>
<itemline>
<description>Red catA OK</description>
<include>Y</include>
<colour>RED</colour>
<category>catA</category>
<status>OK</status>
</itemline>
not(contains($myValues, concat('|',myElem, '|')))