2015-11-02 15 views
0

您好,我正在尝试获取处理指令中的伪属性的值,并将该伪属性的值放入该处理的每个实例的键值中指令。使用使用属性中的处理指令的字符串值<xsl:key>

这样的处理指令的例子看起来像这样的XML内容:

<?uspc-assoc-workflow sws='C139995_131101' forum='2014/12' target='9S1' type='PCA'?> 

我的模板的下面是一个例子。我只看到了使用实际属性的模板实例,但由于这是一个处理指令,因此我不能简单地使用@sws作为值,所以我尝试使用以下命令来捕获字符串中伪属性的值:

<xsl:key name="uspcassocworkflow" match="processing-instruction('uspc-assoc-workflow')" use="substring-before(substring-after(., 'sws=&quot;'), '&quot;')"/> 

此编程是否有效?

我有第二组处理指令的所谓USPC-最终添加这个样子:

<?uspc-end-add id='f104450-r0001' sws='C139995_131101' symbols='yes'?> 

和模板来匹配它看起来像这样:

<xsl:template match="processing-instruction('uspc-end-add')" name="end_add"> 
    <!-- Need to be able to get the key values of the uspc-assoc-workflow sws psudo attribute here below --> 
    <xsl:for-each select="key('uspcassocworkflow', '@sws')"> 
    <!-- Then here I need to be able test if the variable value endswsnumber matches one of uspassocworkflow processing instructions key value --> 
    </xsl:for-each> 
    <xsl:variable name="id"><xsl:value-of select="substring-before(substring-after(., 'id=&quot;'), '&quot;')"/></xsl:variable> 
    <xsl:variable name="endswsnumber"><xsl:value-of select="substring-before(substring-after(., 'sws=&quot;'), '&quot;')"/></xsl:variable> 
    <xsl:variable name="symbol"><xsl:value-of select="substring-before(substring-after(., 'symbol=&quot;'), '&quot;')"/></xsl:variable> 

因此以上我正在尝试将这些关键值引入此模板,然后我想测试变量endswsnumber是否与uspc-assoc-workflow PI的其中一个关键值相匹配。如果他们是匹配的,并且一旦我有相应的uspc-assoc-workflow PI。我将测试相应的uspc-assoc-workflow psudo属性的值是否为“目标”,并为每个目标值输出不同的跨度。因此,如果相应的uspc-assoc-workflow的目标值是'9S1',如上例所示,如果值为'9S2',我将输出一个跨度html,并在html中输出不同的跨度。 “目标”psudo属性只有几个不同的值,所以我对每个属性都进行测试。

谢谢。

+0

你不能用'@sws “无论如何!你必须认为它是一个字符串。此外,单引号用'''表示,而不是用'"'(用于双引号)。您是否可以通过完整的XSLT将您的问题最小化为只有一个期望的输出,并且我们可以对其进行更新以实现输出。 –

+0

我可以使用和后匹配值'sws ='并在关闭“'”之前将其添加到uspc-end-add PI中相同的对应值,而不事先知道实际字符串的值? – LearningandBurning

回答

0

我希望下面的例子将引导您正确的:

要选择在下面输入XML的处理指令:

<root> 
    <?uspc-assoc-workflow sws='C139995_131101' forum='2014/12' target='9S1' type='PCA'?> 
</root> 

您可以使用使用值C139995_131101关键以下样式表选择PI:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
<xsl:output method="xml" encoding="UTF-8" indent="yes" /> 

<xsl:key name="uspcassocworkflow" match="processing-instruction('uspc-assoc-workflow')" use="substring-before(substring-after(., &quot;sws=&apos;&quot;), &quot;&apos;&quot;)"/> 

<xsl:template match="/"> 
    <output> 
     <xsl:copy-of select="key('uspcassocworkflow', 'C139995_131101')"/> 
    </output> 
</xsl:template> 

</xsl:transform> 

给出以下输出:

<?xml version="1.0" encoding="UTF-8"?> 
<output><?uspc-assoc-workflow sws='C139995_131101' forum='2014/12' target='9S1' type='PCA'?></output> 

这里,密钥找到作为sws='之后和'之前的子串的PI uspc-assoc-workflow

check it out here

+0

你好Lingamurthy,感谢您的快速回复。但是不是使用实际值并指定我想要的密钥(在这种情况下为C139995_131101),我希望能够使用'sws'psudo属性的值与uspc-add-end PI psudo属性匹配'sws'作为一个变量。将会有多个uspc-assoc-workflow PI和多个uspc-add-end PI,我不知道除了它们每个都具有相应的匹配'sws'值外,'sws'值将会是什么。那么是否有可能在你的例子'C139995_131101'中变成一个匹配的变量值? – LearningandBurning

0

如果你有机会获得撒克逊9的商用版本之一然后再考虑使用扩展功能saxon:get-pseudo-attributehttp://saxonica.com/html/documentation/functions/saxon/get-pseudo-attribute.html)它提供:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" 
    xmlns:saxon="http://saxon.sf.net/" 
    exclude-result-prefixes="saxon"> 

    <xsl:template match="processing-instruction('uspc-assoc-workflow')"> 
     <xsl:variable name="sws" select="saxon:get-pseudo-attribute('sws')"/> 
     <xsl:value-of select="$sws"/>   
    </xsl:template>  

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

</xsl:transform>