需要XSLT来重构XML节点

时间:2014-01-09 09:10:32

标签: xslt

我需要一个XSLT来根据一个条件重构XML。这是要求。

这是源XML,其中有两个节点,一个是标题1,第二个是标题2

 <?xml version="1.0" encoding="UTF-8"?>
<Data>
<header1>
    <Id>a3s600000004Vi3AAE</Id>
    <field1>a3n600000008X1ZAAU</field1>
    <field2>0066000000Jv4RWAAZ</field2>
    <field3>10121226</field3>
    <field4>Alexis Camacho</field4>
</header1>
<header1>
    <Id>a3s600000004Vi4AAE</Id>
    <field1>a3n600000008PjbAAE</field1>
    <field2>0066000000Jv0m1AAB</field2>
    <field3>07043A</field3>
    <field4>Jose M Rodriguez</field4>
</header1>
<header2>
    <field5>a086000000DA5HpAAL</field5>
    <field6>a3s600000004Vi3AAE</field6>
</header2>
<header2>
    <field5>a086000000DA5HqAAL</field5>
    <field6>a3n600000008PjbAAE</field6>
</header2>

这是目标XML,在这里只有一个节点包含所有7个字段,条件为字段5的值应该等于ID

 <?xml version="1.0" encoding="UTF-8"?>
 <Data>
 <header>
    <Id>a3s600000004Vi3AAE</Id>
    <field1>a3n600000008X1ZAAU</field1>
    <field2>0066000000Jv4RWAAZ</field2>
    <field3>10121226</field3>
    <field4>Alexis Camacho</field4>
   <field5>a3s600000004Vi3AAE</field5>
    <field6>a086000000DA5HpAAL</field6>
</header>
<header>
    <Id>a3s600000004Vi4AAE</Id>
    <field1>a3n600000008PjbAAE</field1>
    <field2>0066000000Jv0m1AAB</field2>
    <field3>07043A</field3>
    <field4>Jose M Rodriguez</field4>
    <field5>a3s600000004Vi4AAE</field5>
    <field6>a086000000DA5HqAAL</field6>
</header>

对于转换,条件是字段5的值应该等于ID。所以请提供XSLT来进行操作。

我已尝试使用以下XSLT仅打印字段5:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Items</h2>
<table border="1">
  <tr bgcolor="#9acd32">
    <th style="text-align:left">Opportunities_id</th>
  </tr>
  <xsl:for-each select="Data/header1">
  <xsl:value-of select="count(.)"/>
  <xsl:if test="count(.) &gt; 0">
  <xsl:variable name="oppId" select="string(/Data/header1/Id)"/>
                <xsl:element name="EXPORTRESPONSE"        namespace="fieldpoint.com/namespaces">; <xsl:for-each select="//Data/header2">
                        <xsl:if test="count(.) &gt; 0">
                            <xsl:variable name="opprId"     select="string(/Data/header2/field5)"/>                             
                            <xsl:if test="compare($opprId,$oppId)=0">asda
                            <Opportunities_id><xsl:value-of    select="$opprId"/></Opportunities_id>
                            </xsl:if>
                      </xsl:if>
                   </xsl:for-each>
               </xsl:element>
   </xsl:if>
   </xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

但它不成功。请说明我做错了什么

提前致谢。

2 个答案:

答案 0 :(得分:0)

最后我能用这个XSLT

做到这一点
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
<xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Target>
    <xsl:for-each select="/Data/header1">
    <header>
            <Id><xsl:value-of select="Id"/></Id>
            <loan__Loan_Account__c><xsl:value-of select="loan__Loan_Account__c"/></loan__Loan_Account__c>
            <loan__Loan_Account__c.Opportunity.Id><xsl:value-of select="loan__Loan_Account__c.Opportunity.Id"/></loan__Loan_Account__c.Opportunity.Id>
            <loan__Loan_Account__c.Opportunity.Account_Number__c><xsl:value-of select="loan__Loan_Account__c.Opportunity.Account_Number__c"/>
            </loan__Loan_Account__c.Opportunity.Account_Number__c>
            <loan__Loan_Account__c.Opportunity.Account.Account_Name__c><xsl:value-of select="loan__Loan_Account__c.Opportunity.Account.Account_Name__c"/>
            </loan__Loan_Account__c.Opportunity.Account.Account_Name__c>
 <xsl:variable name="idValue" select="Id"/>
    <xsl:for-each select="/Data/header2">
           <xsl:variable name="oppValue" select="Opportunities_id"/>
                 <xsl:if test="$idValue = $oppValue"> 
                        <Opportunities_id><xsl:value-of select="$oppValue"/></Opportunities_id>
                        <Properties_id><xsl:value-of select="Properties_id"/></Properties_id>
                </xsl:if> 
  </xsl:for-each>
</header>
   </xsl:for-each>
       </Target>
  </xsl:template>
 </xsl:stylesheet>

答案 1 :(得分:-1)

我删除了一些代码,但您可以在此实现逻辑。

尝试一次:

XML(如您所提供):

<?xml version="1.0" encoding="UTF-8"?>
<Data>
 <header>
    <Id>a3s600000004Vi3AAE</Id>
    <field1>a3n600000008X1ZAAU</field1>
    <field2>0066000000Jv4RWAAZ</field2>
    <field3>10121226</field3>
    <field4>Alexis Camacho</field4>
   <field5>a3s600000004Vi3AAE</field5>
    <field6>a086000000DA5HpAAL</field6>
</header>
<header>
    <Id>a3s600000004Vi4AAE</Id>
    <field1>a3n600000008PjbAAE</field1>
    <field2>0066000000Jv0m1AAB</field2>
    <field3>07043A</field3>
    <field4>Jose M Rodriguez</field4>
    <field5>a3s600000004Vi4AAE</field5>
    <field6>a086000000DA5HqAAL</field6>
</header>
</Data>

<强> XSLT:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Items</h2>
<table border="1">
  <tr bgcolor="#9acd32">
    <th style="text-align:left">Opportunities_id</th>
  </tr>
    <xsl:for-each select="Data/header">
        <tr>
            <td>
                <xsl:if test="Id/text() = field5/text()">
                    <xsl:value-of select="field4/text()" />
                </xsl:if>
            </td>
        </tr>
    </xsl:for-each>    
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

经过测试:http://www.xslfiddle.net/

注意:这不是您所需要的,但您的问题已在此解决。祝你好运!