如何使用XSL更改xml的值

时间:2016-10-18 15:05:10

标签: xslt xslt-1.0

我有一个XML:

  <?xml version="1.0" encoding="UTF-8"?>
<COLLECTION>
<AddedParts NAME="AddedParts" TYPE="Unknown" STATUS="0">
<Part>
<ProcurementType>make</ProcurementType>
<CountryOfOrigin/>
</Part>
</AddedParts>

<ChangedParts NAME="ChangedParts" TYPE="Unknown" STATUS="0">
<Part>
<ProcurementType>make</ProcurementType>
<CountryOfOrigin/>
</Part>
<Part>
<ProcurementType>buy</ProcurementType>
<CountryOfOrigin/>
</Part>
</ChangedParts>
<DeletedParts NAME="DeletedParts" TYPE="Unknown" STATUS="0">
<Part>
<ProcurementType>singlesource</ProcurementType>
<CountryOfOrigin/>
</Part>
<Part>
<ProcurementType>make</ProcurementType>
<CountryOfOrigin/>
</Part>
</DeletedParts>

我想根据 ProcurementType 更改 CountryOfOrigin ProcurementType 。 所需的转换如下 对于ProcurementType

  • make ---&gt;米
  • 购买-----&gt; P
  • singlesource - &gt; P
  • opensource - &gt; P

对于CountryOfOrigin 如果ProcurementType是

  • make - &gt;&gt; MX
  • 购买-----&gt;美国
  • singlesource - &gt;美国
  • opensource - &gt;美国

为此,我申请了。

<xsl:template match="CountryOfOrigin">
        <countryOfOrigin>
            <xsl:choose>
                <xsl:when test="../../Part/ProcurementType='make'">MX</xsl:when>
                <xsl:when test="../../Part/ProcurementType='buy'">US</xsl:when>
                <xsl:when test="../../Part/ProcurementType='singlesource'">US</xsl:when>
                <xsl:when test="../../Part/ProcurementType='opensource'">US</xsl:when>
            </xsl:choose>
        </countryOfOrigin>
    </xsl:template>

    <xsl:template match="Part/ProcurementType">
        <procurementType>
            <xsl:choose>
                <xsl:when test="../../Part/ProcurementType='make'">M</xsl:when>
                <xsl:when test="../../Part/ProcurementType='buy'">P</xsl:when>
                <xsl:when test="../../Part/ProcurementType='singlesource'">P</xsl:when>
                <xsl:when test="../../Part/ProcurementType='opensource'">P</xsl:when>
            </xsl:choose>
        </procurementType>
    </xsl:template>

它不起作用,无论我在XSL中首先应用它,它都被复制为整个内容。 需要帮助

1 个答案:

答案 0 :(得分:1)

怎么样:

<xsl:template match="ProcurementType">
    <procurementType>
        <xsl:choose>
            <xsl:when test=".='make'">M</xsl:when>
            <xsl:when test=".='buy'">P</xsl:when>
            <xsl:when test=".='singlesource'">P</xsl:when>
            <xsl:when test=".='opensource'">P</xsl:when>
        </xsl:choose>
    </procurementType>
</xsl:template>

<xsl:template match="CountryOfOrigin">
    <countryOfOrigin>
        <xsl:choose>
            <xsl:when test="../ProcurementType='make'">MX</xsl:when>
            <xsl:when test="../ProcurementType='buy'">US</xsl:when>
            <xsl:when test="../ProcurementType='singlesource'">US</xsl:when>
            <xsl:when test="../ProcurementType='opensource'">US</xsl:when>
        </xsl:choose>
    </countryOfOrigin>
</xsl:template>

或者,如果您愿意:

<xsl:template match="Part[ProcurementType='make']">
    <Part>
        <ProcurementType>M</ProcurementType>
        <CountryOfOrigin>MX</CountryOfOrigin>
    </Part>
</xsl:template>

<xsl:template match="Part[ProcurementType='buy']">
    <Part>
        <ProcurementType>P</ProcurementType>
        <CountryOfOrigin>US</CountryOfOrigin>
    </Part>
</xsl:template>

<xsl:template match="Part[ProcurementType='singlesource' or ProcurementType='opensource']">
    <Part>
        <ProcurementType>P</ProcurementType>
        <CountryOfOrigin>US</CountryOfOrigin>
    </Part>
</xsl:template>