Xml 同时更改不同的属性

Xml 同时更改不同的属性,xml,xslt,xpath,xslt-1.0,Xml,Xslt,Xpath,Xslt 1.0,在我的XSLT中,我正在预处理大型XML文件,并且必须操作某些值(因为源系统没有按预期交付它们) 属性“name”和“nm”都应该包含相同的文本。 但是,在原始XML中,它们是空的 我需要使用另一个属性“description”和硬编码的查找列表(例如description=“Some value”表示nm,name应该都是“NameABC”)来生成它们。 因为我的查找列表很长,我真的不想在两个模板中实现它,一个用于属性“nm”,另一个用于“name”。 相反,我希望在一个地方实现我的查找列表,

在我的XSLT中,我正在预处理大型XML文件,并且必须操作某些值(因为源系统没有按预期交付它们)

属性“name”和“nm”都应该包含相同的文本。 但是,在原始XML中,它们是空的

我需要使用另一个属性“description”和硬编码的查找列表(例如description=“Some value”表示nm,name应该都是“NameABC”)来生成它们。 因为我的查找列表很长,我真的不想在两个模板中实现它,一个用于属性“nm”,另一个用于“name”。 相反,我希望在一个地方实现我的查找列表,并始终同时更改这两个属性

有没有办法做到这一点

这是我的原始XML(当然是简化的示例):


A.
期望输出:

<?xml version="1.0" encoding="UTF-8"?>
<Sample>
    <Header>
        <Type>A</Type>
    </Header>
    <DataSet name="NameABC">
        <Info description="Some value" other="123" nm="NameABC"/>
    </DataSet>
    <DataSet name="NameXYZ">
        <Info description="Another value" other="456" nm="NameXYZ"/>
    </DataSet>
</Sample>

A.
我当前的XSLT(仅更改属性“nm”):


姓名ABC
NameXYZ
那么:

XSLT1.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:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="DataSet">
    <xsl:variable name="nm">
        <xsl:choose>
            <xsl:when test="Info/@description = 'Some value'">NameABC</xsl:when>
            <xsl:when test="Info/@description = 'Another value'">NameXYZ</xsl:when>
        </xsl:choose>
    </xsl:variable>
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:attribute name="name"><xsl:value-of select="$nm"/></xsl:attribute>
        <xsl:apply-templates select="node()" >
            <xsl:with-param name="nm" select="$nm"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

<xsl:template match="Info">
    <xsl:param name="nm"/>
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:attribute name="nm"><xsl:value-of select="$nm"/></xsl:attribute>
        <xsl:apply-templates select="node()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
<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:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="DataSet/@name">
    <xsl:attribute name="name">
        <xsl:call-template name="getName">
            <xsl:with-param name="desc" select="../Info/@description"/>
        </xsl:call-template>
    </xsl:attribute>
</xsl:template>

<xsl:template match="Info/@nm">
        <xsl:attribute name="nm">
            <xsl:call-template name="getName">
                <xsl:with-param name="desc" select="../@description"/>
            </xsl:call-template>
        </xsl:attribute>
</xsl:template>

<xsl:template name="getName">
    <xsl:param name="desc"/>
    <xsl:choose>
        <xsl:when test="$desc = 'Some value'">NameABC</xsl:when>
        <xsl:when test="$desc = 'Another value'">NameXYZ</xsl:when>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

姓名ABC
NameXYZ

备选方案: XSLT1.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:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="DataSet">
    <xsl:variable name="nm">
        <xsl:choose>
            <xsl:when test="Info/@description = 'Some value'">NameABC</xsl:when>
            <xsl:when test="Info/@description = 'Another value'">NameXYZ</xsl:when>
        </xsl:choose>
    </xsl:variable>
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:attribute name="name"><xsl:value-of select="$nm"/></xsl:attribute>
        <xsl:apply-templates select="node()" >
            <xsl:with-param name="nm" select="$nm"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

<xsl:template match="Info">
    <xsl:param name="nm"/>
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:attribute name="nm"><xsl:value-of select="$nm"/></xsl:attribute>
        <xsl:apply-templates select="node()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
<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:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="DataSet/@name">
    <xsl:attribute name="name">
        <xsl:call-template name="getName">
            <xsl:with-param name="desc" select="../Info/@description"/>
        </xsl:call-template>
    </xsl:attribute>
</xsl:template>

<xsl:template match="Info/@nm">
        <xsl:attribute name="nm">
            <xsl:call-template name="getName">
                <xsl:with-param name="desc" select="../@description"/>
            </xsl:call-template>
        </xsl:attribute>
</xsl:template>

<xsl:template name="getName">
    <xsl:param name="desc"/>
    <xsl:choose>
        <xsl:when test="$desc = 'Some value'">NameABC</xsl:when>
        <xsl:when test="$desc = 'Another value'">NameXYZ</xsl:when>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

姓名ABC
NameXYZ

在这种情况下,我更喜欢使用外部XML文件来实现查找表

<lut>
  <entry desc="Some value" name="NameABC" number="123"/>
  <entry desc="Another value" name="NameXYZ" number="456"/>
  <!-- and another dozen entries -->
</lut>
标题
值也可以很容易地实现依赖关系:使用另一个属性或将
条目
分组到“type”块中

<xsl:template match="DataSet">
  <xsl:variable name="desc" select="./Info/@description"/>
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:attribute name="name">
      <xsl_value-of select="document('lut.xml')/lut/entry[@desc=$desc]/@name"/>
    </xsl:attribute>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="Info">
  <xsl:variable name="desc" select="./@description"/>
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:attribute name="nm">
       <xsl:value-of select="document('lut.xml')/lut/entry[@desc=$desc]/@name"/>
    </xsl:attribute>
    <xsl:attribute name="other">
       <xsl:value-of select="document('lut.xml')/lut/entry[@desc=$desc]/@number"/>
    </xsl:attribute>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>