我是XSLT的新手 我的输入XML为休闲:
dbWriteTable(con, name="data", value=r.memory.data, overwrite=T)
我希望移动<?xml version="1.0" encoding="UTF-8"?>
<response>
<system>Amisys</system>
<FindReceiptDetailARRequest>
<LegacySystem>Amisys</LegacySystem>
<LegacyUserID>ratna</LegacyUserID>
<LegacyPassword>ratna</LegacyPassword>
<OtherLogin />
<OtherPassword />
<AddSecurLogin />
<AddSecurPassword />
<businessArea>PQAA</businessArea>
<GroupNumber>KISHG2D2</GroupNumber>
<receiptNumber>OKEY00000698</receiptNumber>
<billTo>INDIVIDUAL</billTo>
<accountId>CR1277C01</accountId>
<GroupId>KISHG2</GroupId>
<METHOD>POST</METHOD>
<eao>08212015</eao>
</FindReceiptDetailARRequest>
<results>
<ver:receiptDetailHistoryRS xmlns:ver="version4">
<transactionSuccess>true</transactionSuccess>
<accountName>NEW CONTRACT TIMEI</accountName>
<ver:receiptDetails>
<ver:receiptDetail>
<appliedAmount>0</appliedAmount>
<divisionNumber>127776DIV3</divisionNumber>
<invoiceNumber />
<month>2015/02</month>
<processedDate>2015-07-28T00:00:00-04:00</processedDate>
<receiptNumber>OKEY00000699</receiptNumber>
<unappliedAmount>187.00</unappliedAmount>
</ver:receiptDetail>
<ver:receiptDetail>
<appliedAmount>190.00</appliedAmount>
<divisionNumber>127776DIV3</divisionNumber>
<invoiceNumber />
<month>2015/02</month>
<processedDate>2015-07-28T00:00:00-04:00</processedDate>
<receiptNumber>OKEY00000698</receiptNumber>
<unappliedAmount>0</unappliedAmount>
</ver:receiptDetail>
</ver:receiptDetails>
<receiptDetailHistorySC>
<accountId>CR1277C01</accountId>
<billTo>INDIVIDUAL</billTo>
<divisionNumber />
<invoiceNumber />
<receiptNumber />
</receiptDetailHistorySC>
</ver:receiptDetailHistoryRS>
</results>
</response>
下的所有节点以分隔新节点<ver:receiptDetails>
。
我的结果XML应该看起来像休闲
<prvAssocList>
我正在尝试使用XSLT但不能为我工作。你能帮忙吗?
<?xml version="1.0" encoding="UTF-8"?>
<FindReceiptDetailScreenResults>
<prvAssocList>
<prvAssocInfo>
<prvAssocCode>OKEY00000699</prvAssocCode>
</prvAssocInfo>
<prvAssocInfo>
<prvAssocCode>OKEY00000698</prvAssocCode>
</prvAssocInfo>
</prvAssocList>
</FindReceiptDetailScreenResults>
答案 0 :(得分:2)
当你说“不工作”时,你实际上是在收到错误吗?
问题在于这一行:
<xsl:for-each select="/response/results/ver:receiptDetailHistoryRS/ver:receiptDetails/ver:receiptDetail[1]">
您尚未在XSLT中声明名称空间前缀ver
,因此我希望XSLT失败并显示错误Undeclared namespace prefix {ver}
。仅仅因为名称空间前缀在XML中声明为ver
,如果要访问XML中的节点,您仍需要在XSLT中声明它。
因此,您需要做的第一件事是在XSLT中声明名称空间前缀
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:ver="version4">
下一个问题是你已将[1]
放在表达式的末尾。这意味着表达式只会获得第一个receiptDetail
元素(或者更确切地说,如果有多个receiptDetail
节点,它将获得每个receiptDetails
节点下的第一个receiptDetails
。
试试这个XSLT(我已经删除fault
上的检查只是为了简化事情)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:ver="version4" exclude-result-prefixes="ver">
<xsl:output method="xml" indent="yes" />
<xsl:variable name="SYS" select="//system" />
<xsl:template match="/">
<FindReceiptDetailScreenResults>
<xsl:apply-templates select="//results" />
</FindReceiptDetailScreenResults>
</xsl:template>
<xsl:template match="results">
<xsl:choose>
<xsl:when test="$SYS = 'Amisys'">
<prvAssocList>
<xsl:for-each select="/response/results/ver:receiptDetailHistoryRS/ver:receiptDetails/ver:receiptDetail[1]">
<prvAssocInfo>
<prvAssocCode>
<xsl:value-of select="receiptNumber" />
</prvAssocCode>
</prvAssocInfo>
</xsl:for-each>
</prvAssocList>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>