2014-09-23 31 views
-2

我试图查看输入objectType,value0是否存在于Types/Type中,并检查条件以在输出中使用Type/Type的兄弟属性名称值XML。 下面是输入XML:如何将xml字符串属性分配给xslt中的变量

<?xml version="1.0" encoding="UTF-8"?> 
<outPut> 
    <object> 
    <objectType>TestOne</objectType> 
    <Attributes> 
     <attribute name="value0">codeOne</attribute> 
     <attribute name="value1">35</attribute> 
     <attribute name="value2">35</attribute> 
    </Attributes> 
    <objectType>TestTwo</objectType> 
    <Attributes> 
     <attribute name="value0">codeTwo</attribute> 
     <attribute name="value1">25</attribute> 
     <attribute name="value2">35</attribute> 
    </Attributes> 
    <objectType>TestThree</objectType> 
    <Attributes> 
     <attribute name="value0">codeThree</attribute> 
     <attribute name="value1">25</attribute> 
     <attribute name="value2">3225</attribute> 
     <attribute name="value3">225</attribute> 
    </Attributes> 
    <objectType>TestFour</objectType> 
    <Attributes> 
     <attribute name="value0">codeFour</attribute> 
     <attribute name="value1">25</attribute> 
     <attribute name="value2">35</attribute> 
    </Attributes> 
    <objectType>TestFive</objectType> 
    <Attributes> 
     <attribute name="value0">codeFive</attribute> 
     <attribute name="value1">2</attribute> 
     <attribute name="value2">3225</attribute> 
     <attribute name="value3">225</attribute> 
    </Attributes> 
    <objectType>TestSix</objectType> 
    <Attributes> 
     <attribute name="value0">codSix</attribute> 
     <attribute name="value1">2</attribute> 
     <attribute name="value2">3225</attribute> 
     <attribute name="value3">225</attribute> 
    </Attributes> 
    </object> 
    <Types> 
    <Type> 
     <temp>TestOne</temp> 
     <ID>2847</ID> 
     <Is_Ingest>0</Is_Ingest> 
     <Category>NAV</Category> 
     <CategoryTwo>NAVAid</CategoryTwo> 
     <Class>codeOne</Class> 
     <New_Attribute>nisting</New_Attribute> 
     <New_Value>52</New_Value> 
     <Condition>value1=35</Condition> 
    </Type> 
    <Type> 
     <temp>TestTwo</temp> 
     <ID>2847</ID> 
     <Is_Ingest>0</Is_Ingest> 
     <Category>NAV</Category> 
     <CategoryTwo>NAVAid</CategoryTwo> 
     <Class>codeTwo</Class> 
     <New_Attribute>nisting</New_Attribute> 
     <New_Value>53</New_Value> 
     <Condition>value1!=33</Condition> 
    </Type> 
    <Types> 
     <Type> 
     <temp>TestThree</temp> 
     <ID>28247</ID> 
     <Is_Ingest>0</Is_Ingest> 
     <Category>NAV</Category> 
     <CategoryTwo>NAVAid</CategoryTwo> 
     <Class>codeThree</Class> 
     <New_Attribute>nisting</New_Attribute> 
     <New_Value>52</New_Value> 
     <Condition>value1=35</Condition> 
     </Type> 
     <Type> 
     <temp>TestFour</temp> 
     <ID>2847</ID> 
     <Is_Ingest>0</Is_Ingest> 
     <Category>NAV</Category> 
     <CategoryTwo>NAVAid</CategoryTwo> 
     <Class>codeFour</Class> 
     <New_Attribute>AidM</New_Attribute> 
     <New_Value>45</New_Value> 
     <Condition>value1!=33 & value1!=28</Condition> 
     </Type> 
     <Type> 
     <temp>TestFive</temp> 
     <ID>2847</ID> 
     <Is_Ingest>0</Is_Ingest> 
     <Category>NAV</Category> 
     <CategoryTwo>Aid</CategoryTwo> 
     <Class>codeFive</Class> 
     <New_Attribute>AidM</New_Attribute> 
     <New_Value>4</New_Value> 
     <Condition>!value</Condition> 
     </Type> 
     <Type> 
     <temp>TestSix</temp> 
     <ID>2847</ID> 
     <Is_Ingest>0</Is_Ingest> 
     <Category>NAV</Category> 
     <CategoryTwo>Aid</CategoryTwo> 
     <Class>codeSix</Class> 
     <New_Attribute>AidM</New_Attribute> 
     <New_Value>4</New_Value> 
     </Type> 
    </Types> 
    </outPut> 

这里是xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
    <xsl:strip-space elements="*"/> 
    <xsl:template match="/Input"> 
     <objects> 
      <xsl:for-each select="Output"> 
       <object type="{object/objectType}"> 
        <xsl:variable name="attributes" select="Attributes/*"/> 
        <xsl:variable name="matching-template" select="/Output/Types/Type[temp=$attributes and class=$attributes]"/> 
        <template> 
         <xsl:value-of select="$matching-template/New_Attribute"/> 
         <xsl:value-of select="$matching-template/New_Value"/> 
        </template> 
       </object> 
      </xsl:for-each> 
     </objects> 
    </xsl:template> 
</xsl:stylesheet> 

预期输出

<output> 
    <object> 
     <objectType>TestOne</objectType> 
     <Attributes> 
      <attribute name="value0">codeOne</attribute> 
      <attribute name="value1">25</attribute> 
      <attribute name="value2">35</attribute> 
      <attribute name="nisting">52</attribute> 
     </Attributes> 
     <objectType>TestTwo</objectType> 
     <Attributes> 
      <attribute name="value0">codeTwo</attribute> 
      <attribute name="value1">25</attribute> 
      <attribute name="value2">35</attribute> 
      <attribute name="NAV">NAVAID</attribute> 
      <attribute name="value2">35</attribute> 
     </Attributes> 
    </object> 
</output> 

回答

0

注:万一有人想知道这个答案是如何相关的到上面的(编辑)问题:以下是2014年9月23日这个问题的第一个版本被接受的答案。在2014年10月4日,OP几乎完全改变(而不是仅仅发布后续问题),并且OP不接受答案。为方便起见,原题是:

我想解析以下

<points> 
    <point> 
     <A>test=11 or test=12</A> 
     <B>pointer> test!=7847</pointer> 
    </point> 
</points> 

我想要得到的东西一样一样

<out> 
<att>test</att> 
<att2>=</att> 
<att3>or</att3> 
<att4>12</att4> 
</out> 

我已经使用的财产以后
<xsl:variable name="A" select="substring(/points/point/test,1)
它并没有帮助,以获得“或”“和” ......

接受的答案是:

你的XML是无效的,我想这应该是<B><pointer> test!=7847</pointer></B>而不是<B>pointer> test!=7847</pointer>
虽然尚不清楚哪些规则要得到想要的输出中,如果元素名称是静态的或应生成动态的,下面的XSLT将工作:

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

    <xsl:template match="/*"> 
    <xsl:apply-templates select="//A"/> 
    </xsl:template> 

    <xsl:template match="A"> 
    <xsl:variable name="spaceToken" select="tokenize(., ' ')"/> 
    <xsl:variable name="firstEqualToken" select="tokenize($spaceToken[1], '=')"/> 
    <xsl:variable name="secondEqualToken" select="tokenize($spaceToken[3], '=')"/> 
    <xsl:variable name="equalString" select="substring($spaceToken[1],5,1)"/> 
    <out> 
    <att> 
    <xsl:value-of select="$firstEqualToken[1]"/> 
    </att> 
    <att2> 
    <xsl:value-of select="$equalString"/> 
    </att2> 
    <att3> 
    <xsl:value-of select="$spaceToken[2]"/> 
    </att3> 
    <att4> 
    <xsl:value-of select="$secondEqualToken[2]"/> 
    </att4> 
    </out> 
</xsl:template> 
</xsl:stylesheet> 

输出:

<?xml version="1.0" encoding="UTF-8"?> 
<out> 
<att>test</att> 
<att2>=</att2> 
<att3>or</att3> 
<att4>12</att4> 
</out> 

而且添加一些可能更有价值的东西 - 你的选择语句select="substring(/points/point/test,1)将不会像预期的那样工作 - substring()是一个使用字符串的函数,/points/point是xpath的开始,它不会在A文本中获得“测试”。 “test”是名为“A”的节点的文本值的一部分。你会看到文字,例如与xpath //points/point/A/text()。您可以使用substring()-功能如下substring(string, startpoint, length),所以要获得“test = 11”中的“=”$test='test=11'这是substring($test, 5, 1)

参考:W3C - XPath - substring,也许感兴趣的第一个答案在这里提供了很好的解释:XPath - Difference between node() and text()

更新:对于下如何处理!=<B> test!=7847</B>评论中提及的问题 - 它依赖。因为它不清楚1)如果在OP中给出的XML是唯一可能的输入进行处理,或者如果有多种不同的方程式将被拆分,并且输出应该如何以及为什么看起来像建议的输出在OP中。
它已经提到,输出应该“看起来像”建议的输出,但是最好给出整个预期输出,因为我只是想知道为什么<A>test=11 or test=12</A>缺少11。我会预期所需的输出应该代表所有可能的条件,如名称(测试),条件(=),值(s)被检查相等(11和/或12)。也许只有最大值是有趣的?
进一步的问题是:3)如果只有基于相等的条件是可能的 - =!= - 或者如果有可能,例如, <>在输入的条件和4)如果可能的话,有2个以上可选的测试表达式和5)如果这些条件将始终是可选的(使用or)或也可被包括(使用and)。
最后,我不得不承认,我不确定你的意思是“如何改变'='等于和'!='不等于比较值” - 你想要的例子输出<att2>equals</att2>而不是<att2>=</att2>

现在回到在注释的第一个问题 - 如果你应该写第二个模板匹配<B>处理!= - 不知道可能的输入,这并不容易给任何意见。我认为<point>可能有两个以上的条目(C,D ......),并且可能<A>包含使用!=的表达式,并且<B>包含使用=的表达式。因此,我建议使用更动态的方法 - 使用匹配<point>的所有子项的模板,如果包含逻辑运算符(and/or),请检查每个孩子的每个文本,然后首先处理每个表达式(test=12,test!=12)检查是否包含!= - 如果是,则在!=上标记化,如果不是,则检查是否包含=,如果然后标记为=,并生成动态元素,因为给定的<B>条件只会导致3个元素 - <att>test</att> <att2>!=</att> <att>7847</att>,还因为上面的问题4)

如果需要进一步的输入,如果您只是更新您的问题以提供更多信息将会有所帮助;不一定通过回答我提到的所有问题,但至少考虑它们比帮助您获得理想输出更容易。

+0

这是一个很好的帮助。我还有一个问题,我应该为测试创建另一个模板!= 7847?此外,如何将'='等于和'!='更改为不等于比较值。谢谢 – user3983344 2014-09-24 14:30:06

+0

@ user3983344很高兴我已经能够帮助你了。对于你评论中的问题 - 我刚刚更新了我的答案 – 2014-09-24 18:32:09

相关问题