2011-04-15 64 views
0

HI所有模板内的属性,如何使用,如果条件(或者)在XSLT

如何使用,如果条件的属性时,其每次循环打印不同的值。 我有一个名为“AttributeName”的属性。当每次循环时(总共5次),这将采用不同的名称。

我尝试了一些东西..但它没有工作。我的输入XML将不会有任何数据。我所要做的就是每次循环进行5次时打印不同的值。

输入XML:

<?xml version="1.0" encoding="UTF-8"?> 
    </Solution> 
    <Solution> 
     <ID>22060000000000000000000002</ID> 
     <Title>ABCD</Title> 
     <Observations> 
      <Observation>DEF</Observation> 
     </Observations> 
     <ProblemDescription> 1234</ProblemDescription> 
     <ProblemCause>ADDD</ProblemCause> 
     <RepairProcedures> 
      <RepairProcedure>XYZ</RepairProcedure> 
     </RepairProcedures> 
     <ScenarioExplanation> 
      <Scenario>JIJIJIJIJI</Scenario> 
      <Scenario>SCENARIO1.</Scenario> 
     </ScenarioExplanation> 
     <DocumentReferences> 
      <DocRef>NO DATA</DocRef> 
     </DocumentReferences> 
    </Solution> 
</Solutions> 

XSLT:

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

<xsl:stylesheet><xsl:template> 
      <xsl:element name="Tables"> 

      <!-- Tables code here --> 

      </xsl:element> 
      <xsl:element name="Relationships"> 
       <xsl:for-each select="Solutions/Solution"> 
        <xsl:if test="RepairProcedures/RepairProcedure!= '' "> 
         <xsl:for-each select="RepairProcedures/RepairProcedure"> 
          <xsl:variable name="vNumCols" select="5"/> 
          <xsl:element name="Relationship"> 
           <xsl:for-each select="position() &lt; vNumCols"> 
            <xsl:attribute name="Action"><xsl:value-of select="'Insert'"/></xsl:attribute> 
            <xsl:attribute name="DestinationKey"><xsl:value-of select="1"/></xsl:attribute> 
            <xsl:attribute name="RelationshipSpec"><xsl:value-of select="'TableName'"/></xsl:attribute> 
            <xsl:attribute name="SourceKey"><xsl:value-of select="1"/></xsl:attribute> 
            <xsl:attribute name="RelCommonKey"><xsl:value-of select="1"/></xsl:attribute> 
            <xsl:attribute name="AttributeName"> 
            <xsl:if test="position() = 1"><xsl:value-of select="CPComments"/></xsl:if> 
            <xsl:if test="position() = 2"><xsl:value-of select="CPConflict"/></xsl:if> 
            </xsl:attribute> 
            <xsl:attribute name="AttributeValue"><xsl:value-of select="1"/></xsl:attribute> 
           </xsl:for-each> 
          </xsl:element> 
         </xsl:for-each> 
        </xsl:if> 
       </xsl:for-each> 
      </xsl:element> 
     </xsl:element> 
    </xsl:element> 
</xsl:template> 
</xsl:stylesheet> 

我通过repairprocedures循环/ repairprocedure是让 “RelationshipSpec” 文本。

预期输出

<Relationships> 
<Relationship Action="Delete" DestinationKey="1" RelationshipSpec="XYZ" SourceKey="2" RelCommonKey="1_2" AttributeName="CPComments" AttributeValue=""/> 
<Relationship Action="Delete" DestinationKey="1" RelationshipSpec="XYZ" SourceKey="2" RelCommonKey="1_2" AttributeName="CPConflict" AttributeValue=""/> 
<Relationship Action="Delete" DestinationKey="1" RelationshipSpec="XYZ" SourceKey="2" RelCommonKey="1_2" AttributeName="CPUserID" AttributeValue=""/> 
<Relationship Action="Delete" DestinationKey="1" RelationshipSpec="XYZ" SourceKey="2" RelCommonKey="1_2" AttributeName="ProcessID" AttributeValue=""/> 
<Relationship Action="Delete" DestinationKey="1" RelationshipSpec="XYZ" SourceKey="2" RelCommonKey="1_2" AttributeName="ProductId" AttributeValue=""/>   
</Relationships> 

在这些5行变化的唯一值是 “的AttributeName”。我试图打印5行与AttributeName是不同的一个关系。

请帮我做这件事。

我使用XSLT 1.0

谢谢 拉姆

+0

如果您可以提供样本输入(您的xml样本根本不包含任何属性)以及您对该输入的预期输出,那么对所有人来说都会更容易。 – qwerty 2011-04-15 05:25:48

+0

@qwerty:我的输入xml只是用于打印RepairProcedure Value。不会有任何属性。我的o/p会有这个价值。我添加了xslt和expectedoutput。但它的替代空白。我认为格式化问题。我尝试了几次,但不知道如何格式化xslt ..;( – Ramm 2011-04-15 05:38:09

+0

您太多地使用。尝试使用代替它。 – TOUDIdel 2011-04-15 08:55:27

回答

0

不沿着线的东西:

<xsl:if test="position() = 1"> 
    <xsl:attribute name="FirstAttribute"> 
     <xsl:value-of select="CPComments"/> 
    </xsl:attribute> 
</xsl:if> 
<xsl:if test="position() = 2"> 
    <xsl:attribute name="SecondAttribute"> 
     <xsl:value-of select="CPConflict"/> 
    </xsl:attribute> 
</xsl:if> 

工作?

我可能更喜欢<xsl:choose>,但它不像它本来会更短。

+0

嗨,Jan在同一行中添加了一个额外的属性,但是我需要在第二个参数中显示“CPConflict”属性排在第一位。 – Ramm 2011-04-15 07:05:43