Xml 在xslt中执行if-else条件匹配
有人能建议我如何使用xslt实现以下目标吗 这是xml输入:Xml 在xslt中执行if-else条件匹配,xml,xslt,Xml,Xslt,有人能建议我如何使用xslt实现以下目标吗 这是xml输入: <output> <header> <someId>ABC123</someId> </header> <results> <product id="A"> <externalId>XYZ666</externalId> <title>some title a</tit
<output>
<header>
<someId>ABC123</someId>
</header>
<results>
<product id="A">
<externalId>XYZ666</externalId>
<title>some title a</title>
</product>
<product id="B">
<externalId>ABC123</externalId>
<title>some title b</title>
</product>
<product id="C">
<externalId>666777</externalId>
<title>some title c</title>
</product>
</results>
</output>
我需要的是一个xslt,它将只返回具有匹配\header\someId的\product\externalId的产品,否则它将匹配所有\product元素
非常感谢您的任何建议。关于学校的教程是一个很好的起点 试着看看第页 显示如何使用键交叉引用节点。您应该使用 对于XML,我提供了示例XSLT代码来有条件地选择节点
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="results/product">
<xsl:choose>
<xsl:when test="externalId = /output/header/someId">
<!--Here goes the code-->
<something></something>
</xsl:when>
<xsl:otherwise>
<!--Other code -->
<somethingelse></somethingelse>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
上述代码的行为类似
if ("externalId = /output/header/someId") then
<!--Here goes the code-->
<something></something>
else
<!--Other code -->
<somethingelse></somethingelse>
使用标识模板覆盖
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="results/product[externalId = /output/header/someId]">
<!--Here goes the code-->
<something></something>
</xsl:template>
<xsl:template match="results/product[externalId != /output/header/someId]">
<!--Here goes the code-->
<somethingelse></somethingelse>
</xsl:template>
</xsl:stylesheet>
就这么简单-没有变量,没有xsl:select,没有xsl:when,没有xsl:other:
我更喜欢身份覆盖,而不是!!:@婴儿程序员“Aravind”,紧凑性较短,线/运动部件的数量较少,这是DRY Don’t Repeat Yourself原则的要求之一,这是SOLID编程范例中的D。这也是“保持简单,愚蠢”原则的要求。好处是更好的可读性和可理解性,更少的易出错性和更易于维护。同意!我想补充一点:select=Node的副本不需要模板覆盖!示例:在上面的代码中,如果我添加它,它根本不会被执行!!因此,根据需要,我们可以相应地切换到模板覆盖或副本。。如果我错了,请纠正我:@InfantProgrammer'Aravind',我总是喜欢答案中所示的XPath表达式。至于vs.xsl:apply模板的副本,您的论点是正确的。我使用xsl:copy of only来输出评估结果——当然,如果这个结果只在内部需要,就不会将它复制到输出中。这里重要的是XPath表达式。如何使用它取决于OP没有提供给我们的信息,这是不必要的,因为它会掩盖这个问题的普遍性。请不要链接到w3schools,这在世界范围内很不受欢迎。请查看原因。你可能已经链接到了一个包含正确信息的页面,但是这个网站有很多不正确和误导性的信息——并且链接到w3schools以任何方式给了它不值得的可信度。如果你使用谷歌搜索,并且你有一个谷歌帐户,你可以从W3Schoolsorry,我在匹配模式中有一个输入错误,我现在就纠正它。
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="results/product[externalId = /output/header/someId]">
<!--Here goes the code-->
<something></something>
</xsl:template>
<xsl:template match="results/product[externalId != /output/header/someId]">
<!--Here goes the code-->
<somethingelse></somethingelse>
</xsl:template>
</xsl:stylesheet>
<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:template match="/">
<xsl:copy-of select=
"/*[header/someId]/results/product[externalId=/*/header/someId]
|
/*[not(header/someId)]/results/product
"/>
</xsl:template>
</xsl:stylesheet>
<output>
<header>
<someId>ABC123</someId>
</header>
<results>
<product id="A">
<externalId>XYZ666</externalId>
<title>some title a</title>
</product>
<product id="B">
<externalId>ABC123</externalId>
<title>some title b</title>
</product>
<product id="C">
<externalId>666777</externalId>
<title>some title c</title>
</product>
</results>
</output>
<product id="B">
<externalId>ABC123</externalId>
<title>some title b</title>
</product>
<output>
<results>
<product id="A">
<externalId>XYZ666</externalId>
<title>some title a</title>
</product>
<product id="B">
<externalId>ABC123</externalId>
<title>some title b</title>
</product>
<product id="C">
<externalId>666777</externalId>
<title>some title c</title>
</product>
</results>
</output>
<product id="A">
<externalId>XYZ666</externalId>
<title>some title a</title>
</product>
<product id="B">
<externalId>ABC123</externalId>
<title>some title b</title>
</product>
<product id="C">
<externalId>666777</externalId>
<title>some title c</title>
</product>
Exp1[someCondition] | Exp2[not(someCondition)]