Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml xslt是检查条件的有效方法_Xml_Xslt 1.0 - Fatal编程技术网

Xml xslt是检查条件的有效方法

Xml 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

我只是想检查是否有任何方法可以避免像下面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-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>