简体   繁体   English

如何使用XSLT 1.0在某些条件下限制for-each循环?

[英]How to restrict a for-each loop with some condition using XSLT 1.0?

I have this XML code and I want my for loop to run/display only values for specific type num- 10,20,180 我有此XML代码,我希望我的for循环仅运行/显示特​​定类型num-10.20180的值

<input>
<name>Jack</name>
<age>23</age>
<type-10-num>1</type-10-num>
<type-20-num>2</type-20-num>
<type-20-char>3</type-20-char>
<type-180-num>4</type-180-num>
<type-180-char>5</type-180-char>
<type-180-str>6</type-180-str>
</input>

I'm running a for-each loop for checking the type node- 我正在运行一个for-each循环以检查类型node-

<xsl:for-each select="exslt:node-set($input)/*[starts-with(name(),'type-')]">

And fetching the type value from it in a variable- 然后从变量中获取类型值-

 <xsl:variable name="fetchValue">               
                        <xsl:value-of select="substring-before(substring-after(name(), '-'), '-')" />                   
                    </xsl:variable>

But I want my for loop to run one time for each values 10,20,180. 但是我希望我的for循环为每个值10、20、180运行一次。 If type-20 occurs 2 times I want it to run one time for each 20 and then go to next 180. So total it should run 3 times or lets just say I want to print some details related to these 3 values (so it should not repeat). 如果类型20出现2次,我希望它每20次运行一次,然后转到下一个180。因此总计应该运行3次,或者说我想打印一些与这3个值相关的详细信息(因此应该不重复)。

You can use substring-after two times to check for an ending num . 您可以substring-after两次substring-after使用substring-after检查结尾num A separate variable is not necessary. 不需要单独的变量。

<xsl:for-each select="exslt:node-set($input)/*[starts-with(name(),'type-') and substring-after(substring-after(name(),'-'),'-') = 'num']">
  <xsl:value-of select="."/> </xsl:text>
</xsl:for-each>

Output is: 输出为:

1 2 4

If you want to match only these exact names, you could select them directly: 如果只想匹配这些确切名称,则可以直接选择它们:

<xsl:for-each select="exslt:node-set($input)/*[self::type-10-num or self::type-20-num or self::type-180-num]">
  <xsl:value-of select="."/><xsl:text> </xsl:text>
</xsl:for-each>

Output is the same. 输出是相同的。

This transformation : 此转换

<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=
        "*[starts-with(name(), 'type-')
         and substring(name(), string-length(name())-2) = 'num'
         ]">
    <xsl:copy-of select="."/>
  </xsl:template>

  <xsl:template match="text()"/>
</xsl:stylesheet>

when applied on the provided XML document (formatted for readability): 当应用于提供的XML文档 (为便于阅读而格式化)时:

<input>
    <name>Jack</name>
    <age>23</age>
    <type-10-num>1</type-10-num>
    <type-20-num>2</type-20-num>
    <type-20-char>3</type-20-char>
    <type-180-num>4</type-180-num>
    <type-180-char>5</type-180-char>
    <type-180-str>6</type-180-str>
</input>

Produces data for each of the elements with name type-XYZ-num: 为名称类型为XYZ-num的每个元素生成数据

<type-10-num>1</type-10-num>
<type-20-num>2</type-20-num>
<type-180-num>4</type-180-num>

One can replace this code in the matching template: 可以在匹配的模板中替换此代码:

<xsl:copy-of select="."/>

with whatever is necessary in the particular problem being solved. 在解决特定问题上有什么必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM