如何获取此xml标记属性值的前一个同胞计数?

时间:2022-04-15 09:03:36

I want to get the count of ProcessId which is having value 1234.Current foucus element is PlayBack

我想要得到ProcessId的计数,它的值是1234。当前的foucus元素正在回放

<WorkFlow>
<Step>
<PlayBack>
<AppInfo ProcessId="1234"/>
</PlayBack>
</Step>
<Step>
<PlayBack>
<AppInfo ProcessId="1234"/>
</PlayBack>
</Step>
<Step>
<PlayBack>
<AppInfo ProcessId="1284"/>
</PlayBack>
</Step>
</WorkFlow>

I tried this code but don't know where i am doing wrong

我尝试了这个代码,但不知道我哪里做错了

Here $ActivePID=1234 and $AppVaribale is some value i need to print if the ProcessID is unique
 <xsl:if test="((count(preceding-sibling::Step/PlayBack/AppInfo[@ProcessID= $ActivePID])= 0) and (GeneralInfo/@AdaptorID = 1))">
  <xsl:text>&#xa;</xsl:text>
  <xsl:value-of select ="$AppVaribale"/>
  <xsl:text>&#xa;</xsl:text>
</xsl:if>

3 个解决方案

#1


3  

If you are indeed positioned on a PlayBack element, the expression you need is this

如果您确实位于回放元素上,那么您需要的表达式是这样的

<xsl:if test="count(../preceding-sibling::Step/PlayBack/AppInfo[@ProcessId = $ActivePID]) = 0">

Note the use of .. to get the parent Step element, as PlayBack itself does not have any siblings. Also note your current expression referred to ProcessID not ProcessId (XML is case-sensitive)

注意…的用法。为了获得父步骤元素,回放本身没有任何兄弟元素。还要注意,您当前的表达式引用ProcessID而不是ProcessID (XML是大小写敏感的)

However, this is not actually the most efficient approach. Perhaps you need to read up on a technique called Muenchian Grouping, which can be used to find the first occurrence of each value.

然而,这并不是最有效的方法。也许您需要阅读一种叫做Muenchian分组的技术,它可以用来查找每个值的第一个出现。

You would define a key like so

你可以这样定义一个键

<xsl:key name="Processes" match="PlayBack" use="AppInfo/@ProcessId" />

Then your expression is written like this

你的表达式是这样写的

<xsl:if test="generate-id() = generate-id(key('Processes', $ActivePID)[1])">

Try this XSLT

试试这个XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" />
    <xsl:key name="Processes" match="PlayBack" use="AppInfo/@ProcessId" />
    <xsl:template match="PlayBack">
        <xsl:variable name="ActivePID" select="AppInfo/@ProcessId" />
        <xsl:if test="generate-id() = generate-id(key('Processes', $ActivePID)[1])">
            <xsl:text>&#xa;</xsl:text>
            <xsl:value-of select ="'Test'"/>
            <xsl:text>&#xa;</xsl:text>
        </xsl:if>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Note that if you actually wanted to find elements that only occurred once (rather than the first occurrence of each element) you can change the expression to this:

请注意,如果您实际上想要找到只发生一次的元素(而不是每个元素的第一次出现),您可以将表达式更改为:

<xsl:if test="count(key('Processes', $ActivePID)[1]) = 1">

#2


1  

@Jak,

@Jak,

USe below XSL

使用以下XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name = "Count" >
<xsl:value-of select = "count(WorkFlow/Step/PlayBack/AppInfo[@ProcessId =  1234])"/>
</xsl:variable>
<test>
<xsl:value-of select ="$Count"/>
</test>
</xsl:template>
</xsl:stylesheet>

Just to make sure , its working .. am adding the count value variable i.e, Count in between Test tag.

我只是想确认一下,它是有效的。正在添加count值变量i。e,在测试标签之间进行计数。

#3


1  

You say the "current focus element is PlayBack", but that element doesn't have any preceding siblings named Step (in fact it doesn't have any preceding sibling elements at all). You are missing a ..

您说“当前焦点元素是回放”,但是该元素没有任何之前命名为Step的兄弟元素(实际上它根本没有任何之前的兄弟元素)。你少了一个。

count(../preceding-sibling::Step/PlayBack/AppInfo[@ProcessID=$ActivePID])

#1


3  

If you are indeed positioned on a PlayBack element, the expression you need is this

如果您确实位于回放元素上,那么您需要的表达式是这样的

<xsl:if test="count(../preceding-sibling::Step/PlayBack/AppInfo[@ProcessId = $ActivePID]) = 0">

Note the use of .. to get the parent Step element, as PlayBack itself does not have any siblings. Also note your current expression referred to ProcessID not ProcessId (XML is case-sensitive)

注意…的用法。为了获得父步骤元素,回放本身没有任何兄弟元素。还要注意,您当前的表达式引用ProcessID而不是ProcessID (XML是大小写敏感的)

However, this is not actually the most efficient approach. Perhaps you need to read up on a technique called Muenchian Grouping, which can be used to find the first occurrence of each value.

然而,这并不是最有效的方法。也许您需要阅读一种叫做Muenchian分组的技术,它可以用来查找每个值的第一个出现。

You would define a key like so

你可以这样定义一个键

<xsl:key name="Processes" match="PlayBack" use="AppInfo/@ProcessId" />

Then your expression is written like this

你的表达式是这样写的

<xsl:if test="generate-id() = generate-id(key('Processes', $ActivePID)[1])">

Try this XSLT

试试这个XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" />
    <xsl:key name="Processes" match="PlayBack" use="AppInfo/@ProcessId" />
    <xsl:template match="PlayBack">
        <xsl:variable name="ActivePID" select="AppInfo/@ProcessId" />
        <xsl:if test="generate-id() = generate-id(key('Processes', $ActivePID)[1])">
            <xsl:text>&#xa;</xsl:text>
            <xsl:value-of select ="'Test'"/>
            <xsl:text>&#xa;</xsl:text>
        </xsl:if>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Note that if you actually wanted to find elements that only occurred once (rather than the first occurrence of each element) you can change the expression to this:

请注意,如果您实际上想要找到只发生一次的元素(而不是每个元素的第一次出现),您可以将表达式更改为:

<xsl:if test="count(key('Processes', $ActivePID)[1]) = 1">

#2


1  

@Jak,

@Jak,

USe below XSL

使用以下XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name = "Count" >
<xsl:value-of select = "count(WorkFlow/Step/PlayBack/AppInfo[@ProcessId =  1234])"/>
</xsl:variable>
<test>
<xsl:value-of select ="$Count"/>
</test>
</xsl:template>
</xsl:stylesheet>

Just to make sure , its working .. am adding the count value variable i.e, Count in between Test tag.

我只是想确认一下,它是有效的。正在添加count值变量i。e,在测试标签之间进行计数。

#3


1  

You say the "current focus element is PlayBack", but that element doesn't have any preceding siblings named Step (in fact it doesn't have any preceding sibling elements at all). You are missing a ..

您说“当前焦点元素是回放”,但是该元素没有任何之前命名为Step的兄弟元素(实际上它根本没有任何之前的兄弟元素)。你少了一个。

count(../preceding-sibling::Step/PlayBack/AppInfo[@ProcessID=$ActivePID])