Xslt 用于验证任何xml文档的测试是否失败的样式表';s节点
请提供以下帮助: 我有一个输入文件,它是非常异构和大的 我需要在单独的节点上运行单独的测试,并最终输出一个布尔值:是否有任何测试失败?还是他们都通过了 有什么好办法吗 我已经详细列举了一个例子 输入文件的简化示例:Xslt 用于验证任何xml文档的测试是否失败的样式表';s节点,xslt,Xslt,请提供以下帮助: 我有一个输入文件,它是非常异构和大的 我需要在单独的节点上运行单独的测试,并最终输出一个布尔值:是否有任何测试失败?还是他们都通过了 有什么好办法吗 我已经详细列举了一个例子 输入文件的简化示例: <?xml version="1.0" encoding="utf-8"?> <Application> <PersonApplicants> <Person ApplicantType="B
<?xml version="1.0" encoding="utf-8"?>
<Application>
<PersonApplicants>
<Person ApplicantType="Borrower" ApplicationType="Sole" >
<PersonName FirstName="Jane" Surname="Smith" />
<PersonalDetails CreditHistory="CleanCredit" YearsInCurrentProfession="12">
<Residency ResidencyStatus="Citizen" ResidencyCountry="Australia"/>
</PersonalDetails>
</Person>
<Person ApplicantType="Guarantor" ApplicationType="Sole" >
<PersonName FirstName="John" Surname="Smith" />
</Person>
</PersonApplicants>
<CompanyApplicants>
<Company ApplicantType="Guarantor" ApplicationType="Joint">
<CompanyName CompanyName="C1" TradingName="C1" />
<BusinessDetails YearsInBusiness="19" CreditHistory="CleanCredit" />
</Company>
</CompanyApplicants>
</Application>
在每个节点上运行一系列单独的测试,并在最后输出是否至少一个测试失败或所有测试都通过
输出示例xml:
<TestResult>Failed</TestResult>
失败
如果任何测试失败
或
已通过
如果所有测试都通过了
这是我尝试做的一个示例:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:template match="/">
<TestResult>
<xsl:variable name="fail_test">
<xsl:apply-templates select="Application" />
</xsl:variable>
<xsl:choose>
<!-- If any rule triggers/fails, output the following value "True".-->
<xsl:when test="$fail_test">True</xsl:when>
<!-- Otherwise putput the following value "False"-->
<xsl:otherwise>False</xsl:otherwise>
</xsl:choose>
</TestResult>
</xsl:template>
<xsl:template match="Application">
<xsl:apply-templates select="PersonApplicants"/>
<xsl:apply-templates select="CompanyApplicants"/>
</xsl:template>
<xsl:template match="PersonApplicants">
<!-- Test Rule 1 Have at least one Borrower, Person or Company. -->
<xsl:variable name="var1">
<xsl:choose>
<xsl:when test="//Person/@ApplicantType='Borrower' or //Company/@ApplicantType='Borrower'"><xsl:copy-of select="true()" /></xsl:when>
<xsl:otherwise><xsl:copy-of select="false()" /></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- Test Rule 2 Only one applicant allowed for a Sole application, Person or Company. -->
<xsl:variable name="var2">
<xsl:choose>
<xsl:when test="Person/@ApplicationType='Sole'">
<xsl:choose>
<xsl:when test="count(//Person) + count(//Company) = 1"><xsl:copy-of select="true()" /></xsl:when>
<xsl:otherwise><xsl:copy-of select="false()" /></xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise><xsl:copy-of select="true()" /></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- need a way to handle the results on all Person nodes -->
<!-- this is only in here as a placeholder for the correct code -->
<!-- Test child nodes of Person Applicants, i.e Person nodes. -->
<xsl:for-each select="Person">
<xsl:apply-templates select="."/>
</xsl:for-each>
<!--******************************************************************-->
<!--the returned value of the template-->
<xsl:copy-of select="not($var1 and $var2)" />
<!--******************************************************************-->
</xsl:template>
<xsl:template match="Person">
<!-- @ApplicantType LIXI 2.0 node Validation: is required -->
<xsl:variable name="var1">
<xsl:choose>
<xsl:when test="not (@ApplicantType) or string(@ApplicantType) =''"><xsl:copy-of select="true()" /></xsl:when>
<xsl:otherwise><xsl:copy-of select="false()" /></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- @PrimaryApplicant LIXI 2.0 node Validation: PrimaryApplicant is required when Applicant Type is Borrower -->
<xsl:variable name="var2">
<xsl:choose>
<xsl:when test="not (@PrimaryApplicant) or string(@PrimaryApplicant) =''">
<xsl:choose>
<xsl:when test="@ApplicantType='Borrower'"><xsl:copy-of select="true()" /></xsl:when>
<xsl:otherwise><xsl:copy-of select="false()" /></xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise><xsl:copy-of select="true()" /></xsl:otherwise>
</xsl:choose>
<!--the returned value of the template-->
<xsl:copy-of select="not($var1 and $var2)" />
<!--******************************************************************-->
</xsl:template>
</xsl:stylesheet>
真的
假的
最简单,从您所在的位置开始
<xsl:template match="/">
<TestResult>
<xsl:variable name="fail_test">
<xsl:apply-templates select="Application" />
</xsl:variable>
<xsl:choose>
<xsl:when test="contains($fail_test,'false')" >Failed</xsl:when>
<xsl:otherwise>Passed</xsl:otherwise>
</xsl:choose>
</TestResult>
</xsl:template>
失败
通过
请注意,您的测试结束sylesheet的格式不正确,而且
/xsl:if
应该是/xsl:choose
谢谢,但问题是如何正确获取变量值,而不是在获得变量后如何测试它。更正了错误,ta。如果您按照我的建议修改样式表,它将给出您要求的结果;如果任何单个测试失败,则返回Failed;如果所有测试都通过,则返回Passed。这与你最初的问题有什么不同?我明白你现在的意思;我开始在顶部获取一个布尔值并测试它,而不是一个包含所有测试结果的字符串。非常感谢。
<xsl:template match="/">
<TestResult>
<xsl:variable name="fail_test">
<xsl:apply-templates select="Application" />
</xsl:variable>
<xsl:choose>
<xsl:when test="contains($fail_test,'false')" >Failed</xsl:when>
<xsl:otherwise>Passed</xsl:otherwise>
</xsl:choose>
</TestResult>
</xsl:template>