基于输入值的 xsl 转换

xsl transformation based on value from input

我需要 xml 输出,但是输出 xml 是基于输入值的条件。 输出 xml 不是输入 xml 的转换。 xml 位于文档函数访问的另一个变量中。在这里,我使用文档函数在 xslt 中使用了 xml。

文档函数访问的xml有id属性,其值按升序排列。输入是一个数字(比如来自输入 xml),该数字应该与从顶部选择元素开始访问的文档的 id 属性 xml 进行比较。如果在任何情况下对应的值(id 属性 + 15)大于输入数字,则应从输出中忽略特定的选择元素和前面的选择元素 xml .

例如,如果输入数字是 100,则输出应如下所示,因为满足条件的第一个元素是 id 值“90”。因为90+15大于100.

<menu>
     <choice id="95">C</choice>
     <choice id="100">C</choice>
   </menu>

我的输入xml是说

<a>100</a> 

我的 xslt 输出所有内容

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <my:elements>
   <menu>
     <choice id="20">ABC</choice>
     <choice id="23">B</choice>
     <choice id="30">C</choice>
     <choice id="37">C</choice>
     <choice id="45">C</choice>
     <choice id="50">C</choice>
     <choice id="90">C</choice>
     <choice id="95">C</choice>
     <choice id="100">C</choice>
   </menu>
 </my:elements>

 <xsl:template match="/">
  <xsl:copy-of select="document('')/*/my:elements/*"/>
 </xsl:template>
</xsl:stylesheet>

这是一种方法。将其视为伪代码。您必须插入详细信息。

<!-- Find the id of the first choice that over the limit.  --> 
<xsl:variable name="firstChoiceOverLimitID" select="generate-id(document('')/*/my:elements//choice[number(@id) + 15 &gt; 100][1])"/> 
  
<xsl:template match="/">
  <!-- Output everything after the first choice over the limit.  -->
  <xsl:copy-of select="document('')/*/my:elements//choice[generate-id(.) = $firstChoiceOverLimitID]/following-sibling::choice"/>
</xsl:template>