Xml xslt是检查条件的有效方法
我只是想检查是否有任何方法可以避免像下面xslt1.0中那样的冗长编码,在xslt1.0中,我们有多个检查条件,根据特定条件复制输出元素。如果条件不为true,则输出中将不存在元素本身。我问的原因是,xsl文件中有很多元素 我的xsltXml xslt是检查条件的有效方法,xml,xslt-1.0,Xml,Xslt 1.0,我只是想检查是否有任何方法可以避免像下面xslt1.0中那样的冗长编码,在xslt1.0中,我们有多个检查条件,根据特定条件复制输出元素。如果条件不为true,则输出中将不存在元素本身。我问的原因是,xsl文件中有很多元素 我的xslt <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:output omit-xml-declaratio
<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="/">
<Root>
<xsl:if test="Root/a/text() = '1'">
<first>present</first>
</xsl:if>
<xsl:if test="Root/b/text() = '1'">
<second>present</second>
</xsl:if>
<xsl:if test="Root/c/text() = '1'">
<third>present</third>
</xsl:if>
<xsl:if test="Root/d/text() = '1'">
<fourth>present</fourth>
</xsl:if>
</Root>
</xsl:template>
</xsl:stylesheet>
目前
目前
目前
目前
我的输入xml
<Root>
<a>1</a>
<b>1</b>
<c>0</c>
<d>1</d>
</Root>
1.
1.
0
1.
我的输出
<Root>
<first>present</first>
<second>present</second>
<fourth>present</fourth>
</Root>
目前
目前
目前
第一
第二
第三
第四
目前
当此转换应用于提供的XML文档时:
<Root>
<a>1</a>
<b>1</b>
<c>0</c>
<d>1</d>
</Root>
1.
1.
0
1.
生成所需的正确结果:
<Root>
<first>present</first>
<second>present</second>
<fourth>present</fourth>
</Root>
目前
目前
目前
一种方法是在output-template.xml中为输出创建模板:
<Root>
<first>present</first>
<second>present</second>
<third>present</third>
<fourth>present</fourth>
</Root>
目前
目前
目前
目前
然后处理这个:
<xsl:variable name="input" select="/"/>
<xsl:template match="Root/*">
<xsl:variable name="p" select="position()"/>
<xsl:if test="$input/Root/*[$p] = '1'">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:template>
<xsl:template match="/">
<Root>
<xsl:apply-templates select="document('output-template.xml')/Root/*"/>
</Root>
</xsl:template>
<xsl:variable name="input" select="/"/>
<xsl:template match="Root/*">
<xsl:variable name="p" select="position()"/>
<xsl:if test="$input/Root/*[$p] = '1'">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:template>
<xsl:template match="/">
<Root>
<xsl:apply-templates select="document('output-template.xml')/Root/*"/>
</Root>
</xsl:template>