Xml XSLT-测试多个同级节点,如果任何同级节点与测试匹配,则返回true
谢谢你的帮助。我仍在学习XSLT,所以请随时告诉我我已经想到的应该以不同的方式完成 我正在尝试处理以下XML,并根据几个简单的条件插入带有true或false值的Xml XSLT-测试多个同级节点,如果任何同级节点与测试匹配,则返回true,xml,xslt,Xml,Xslt,谢谢你的帮助。我仍在学习XSLT,所以请随时告诉我我已经想到的应该以不同的方式完成 我正在尝试处理以下XML,并根据几个简单的条件插入带有true或false值的 <?xml version="1.0" encoding="UTF-8"?> <FILE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <HEADER> <LINE> <
<?xml version="1.0" encoding="UTF-8"?>
<FILE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<HEADER>
<LINE>
<LINENO>1</LINENO>
<DOLLARS>100</DOLLARS>
</LINE>
<LINE>
<LINENO>2</LINENO>
<DOLLARS>100</DOLLARS>
<REASON>
<CODE>1</CODE>
</REASON>
</LINE>
<LINE>
<LINENO>3</LINENO>
<DOLLARS>0</DOLLARS>
<REASON>
<CODE>99</CODE>
</REASON>
</LINE>
<LINE>
<LINENO>4</LINENO>
<DOLLARS>0</DOLLARS>
<REASON>
<CODE>99</CODE>
<CODE>1</CODE>
</REASON>
</LINE>
</HEADER>
</FILE>
如果美元>0,则插入false
如果美元=0且代码在值列表中,则插入true
否则插入false
我的问题是有两个(或更多)代码标记作为美元标记的兄弟
这是我到目前为止使用的XSLT
<?xml version='1.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"
omit-xml-declaration="no"/>
<xsl:strip-space elements="*"/>
<!-- global template to copy everything that doesn't match the other templates -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- Find the DOLLARS tag for every line -->
<xsl:template match="DOLLARS">
<xsl:choose>
<xsl:when test="number(../DOLLARS) > 0">
<xsl:copy-of select="."/>
<newtag>false</newtag>
</xsl:when>
<xsl:when test="contains('-1-2-3-4-5-6-', concat('-', ../CODE, '-'))">
<xsl:copy-of select="."/>
<newtag>true</newtag>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
<newtag>false</newtag>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
假的
真的
假的
我知道问题在于contains测试只是查找/测试第一个代码,但我不知道如何让它测试两个代码标记,并在其中一个代码匹配时给我一个true
大卫干得好。我喜欢看到新使用XSLT的用户对每种XSLT使用的都不是
xsl:for
:-)
看起来您只需要为DOLLARS=0添加测试,并将contains()
移动到谓词中
您还可以通过删除对DOLLARS>0
的测试来简化xsl:choose
,因为结果与xsl:others
相同。您还可以将
的xsl:copy移出xsl:choose
例如
<xsl:template match="DOLLARS">
<xsl:copy-of select="."/>
<xsl:choose>
<xsl:when test=".=0 and ../REASON/CODE[contains('-1-2-3-4-5-6-', concat('-', ., '-'))]">
<newtag>true</newtag>
</xsl:when>
<xsl:otherwise>
<newtag>false</newtag>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
如果美元>0,则插入false
如果美元=0且代码在值列表中,则插入
``真的
否则插入false
我相信这可以改写为:
1.插入一个
元素。
2.如果美元=0且代码在值列表中。然后让
的值为真;否则就错了
考虑以下样式表:
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="valid-codes">
<code>1</code>
<code>2</code>
<code>3</code>
<code>4</code>
<code>5</code>
<code>6</code>
</xsl:variable>
<!-- identity transform -->
<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()"/>
<newtag>
<xsl:value-of select="DOLLARS=0 and REASON/CODE=exsl:node-set($valid-codes)/code"/>
</newtag>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
应用于输入示例,结果将是:
<?xml version="1.0" encoding="UTF-8"?>
<FILE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<HEADER>
<LINE>
<LINENO>1</LINENO>
<DOLLARS>100</DOLLARS>
<newtag>false</newtag>
</LINE>
<LINE>
<LINENO>2</LINENO>
<DOLLARS>100</DOLLARS>
<REASON>
<CODE>1</CODE>
</REASON>
<newtag>false</newtag>
</LINE>
<LINE>
<LINENO>3</LINENO>
<DOLLARS>0</DOLLARS>
<REASON>
<CODE>99</CODE>
</REASON>
<newtag>false</newtag>
</LINE>
<LINE>
<LINENO>4</LINENO>
<DOLLARS>0</DOLLARS>
<REASON>
<CODE>99</CODE>
<CODE>1</CODE>
</REASON>
<newtag>true</newtag>
</LINE>
</HEADER>
</FILE>
“我的问题是有两个(或更多)代码标记作为美元标记的子项。”在您的示例中,美元元素没有子项。代码元素是REASON的子元素,REASON是美元的兄弟(为了看到这一点,正确缩进代码有助于)。现在,您确定要将
元素作为美元的子元素插入吗?这将产生混合内容,使下一阶段的处理更加困难。谢谢。@DLoysen-Michael的结果和使用我答案中任一选项的结果基本相同。唯一的区别是newtag
的位置,但它在层次结构中仍然处于同一级别,不会创建混合内容。谢谢。我喜欢将代码放入变量的想法。我的实际转换需要检查4个不同XML标记中的代码,而不必重复包含列表4次是很有吸引力的,即使只是为了可读性。不幸的是,当我在select的值为true时进行测试时,没有返回任何内容。我怀疑exsl在我将其放入的应用程序中不可用。@DLoysen您确定吗?我不知道有哪个XSLT1.0处理器不支持EXSLT node-set()扩展函数。不过,如果这是真的,您可以将代码放在另一个XML文档中,甚至放在样式表本身的不同名称空间下。如果您在这方面需要更多帮助,请告诉我。我对XSLT的了解很少。我为这个问题发布的XML和XSLT是我真正使用的经过净化的版本,所以我完全有可能把事情搞砸了。当我有时间时,我可能会再次尝试你的解决方案,但谁知道什么时候会发生。谢谢你的帮助。
<?xml version="1.0" encoding="UTF-8"?>
<FILE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<HEADER>
<LINE>
<LINENO>1</LINENO>
<DOLLARS>100</DOLLARS>
<newtag>false</newtag>
</LINE>
<LINE>
<LINENO>2</LINENO>
<DOLLARS>100</DOLLARS>
<REASON>
<CODE>1</CODE>
</REASON>
<newtag>false</newtag>
</LINE>
<LINE>
<LINENO>3</LINENO>
<DOLLARS>0</DOLLARS>
<REASON>
<CODE>99</CODE>
</REASON>
<newtag>false</newtag>
</LINE>
<LINE>
<LINENO>4</LINENO>
<DOLLARS>0</DOLLARS>
<REASON>
<CODE>99</CODE>
<CODE>1</CODE>
</REASON>
<newtag>true</newtag>
</LINE>
</HEADER>
</FILE>