仅当源属性存在时,XSLT才会向节点添加属性

Spe*_*ump 3 xslt xpath attributes

我的意思是当且仅当该属性的数据的源存在时,才向属性添加属性元素.

换句话说,如果源与我的规则不匹配,我不希望以空属性结束.

<tok id="t1" fooID=""/> //not accepted
<tok id="t1" /> //instead of ^
<tok id="t1" fooID="bar"/> //accepted
Run Code Online (Sandbox Code Playgroud)


此外,我想测试源是否有超过1个节点对应于我当前的属性,如果是,则添加另一个foo属性"source2".这是我目前正在使用的:

<xsl:template match="tokens/token">
<tok id="{@ID}" 
     ctag="{/root/myStuff/fooSources[1and2 how?]/fooSource[@fooID=current()/@ID]}" 
>
</tok>
Run Code Online (Sandbox Code Playgroud)

源XML是这样的:

<root>
   <myStuff>
      <tokens>
         <token ID="bar"/>
         <token ID="anotherBar"/>
         <token ID="noFoo"/>
      </tokens>

      <fooSources>
         <fooSource fooID="bar"> kitten </fooSource>
         <fooSource fooID="anotherBar"> shovel </fooSource>
      </fooSources>

      <fooSources>
         <fooSource fooID="bar"> kitty </fooSource>
         <fooSource fooID="notAnotherBar"> fridge </fooSource>
      </fooSources>
   </myStuff>
</root>
Run Code Online (Sandbox Code Playgroud)

期望的结果是:

<tok id="bar" fooID="kitten" fooID_2="kitty"/>
<tok id="anotherBar" fooID="shovel"/> 
<tok id="noFoo" /> 
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助!

PS:我想在xpath 1.0中这样做

Max*_*oro 5

<foo>
   <xsl:if test="@bar">
      <xsl:attribute name="id">
         <xsl:value-of select="@bar"/>
      </xsl:attribute>
   </xsl:if>
</foo>
Run Code Online (Sandbox Code Playgroud)