转换xml子节点值

时间:2016-09-20 14:28:14

标签: c# xslt

 - <SAPAccounts> <SAPAccount ChargeNumber="0FPT15068806"
   WSDescription="CCEC 32 - Rotor Standstill Inspection" /> <SAPAccount
   ChargeNumber="dfgdfgfgg" WSDescription="gfghfghfhhs" />
   </SAPAccounts>

我想将xml转换为不同类型的xml。我想访问多个子节点值。

<AddChargeNumberIDto>
<SAP>
<ChargeNumber>0FPT15068806</ChargeNumber>
<Description>CCEC 32 - Rotor Standstill Inspection</Description>
<SAP>
<SAP>
<ChargeNumber>dfgdfgfgg</ChargeNumber>
<Description>gfghfghfhhs</Description>
<SAP>
</AddChargeNumberIDto>

1 个答案:

答案 0 :(得分:0)

以下样式表:

XSLT 1.0

<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="SAPAccounts">
    <AddChargeNumberIDto>
        <xsl:for-each select="SAPAccount">
            <SAP>
                <ChargeNumber>
                    <xsl:value-of select="@ChargeNumber"/>
                </ChargeNumber>
                <Description>
                    <xsl:value-of select="@WSDescription"/>
                </Description>
            </SAP>
        </xsl:for-each>
    </AddChargeNumberIDto>
</xsl:template>

</xsl:stylesheet>

应用于输入示例时,将返回预期结果:

<?xml version="1.0" encoding="UTF-8"?>
<AddChargeNumberIDto>
   <SAP>
      <ChargeNumber>0FPT15068806</ChargeNumber>
      <Description>CCEC 32 - Rotor Standstill Inspection</Description>
   </SAP>
   <SAP>
      <ChargeNumber>dfgdfgfgg</ChargeNumber>
      <Description>gfghfghfhhs</Description>
   </SAP>
</AddChargeNumberIDto>

注意:您的样式表引用OpenJobInfo - 但您的输入中没有此类元素。