XSLT 1.0转换覆盖或添加子节点

时间:2016-11-04 17:27:46

标签: xml visual-studio-2015 xslt-1.0

我正在尝试在以下xml上使用xslt 1.0(使用visual studio 2015)转换。

我得到了众议院的名字,以及应该在众议院内的家具的名称和新价值。

如果已存在具有给定名称的家具,请覆盖其值 如果没有,请创建一个新的 如果家具在家里但不应该被覆盖,只需复制即可 不应改变已经在房子里的家具的顺序。

我不是c ++,C#,JAVA等语言的新手,但xslt是......不同的。 虽然这是上述语言中的一项简单任务,但我在这里真的很挣扎。

请帮忙。

XML代码

<Street>
  <House Name="house1">
    <Window>niceView</Window>
    <Furniture Name="name1">value1</Furniture>
    <Furniture Name="name2">value2</Furniture>
    <Furniture Name="name3">value3</Furniture>
    <Furniture Name="name4">value4</Furniture>     
  </House>
  <House>
    <Brick Name="cubrick"></Brick>
    <Furniture name="name1>value</Furniture>
  </House>
</Street>

XSLT开始

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

<xsl:param name="house1" select='house1'/>
<xsl:param name="name2" select='newValue1'/>
<xsl:param name="name4" select='newValue2'/>
<xsl:param name="name10" select='newValue5'/>
...

所需的输出是:

<Street>
  <House Name="house1">
    <Window>niceView</Window>
    <Furniture Name="name1">value1</Furniture>
    <Furniture Name="name2">newValue1</Furniture>
    <Furniture Name="name3">value3</Furniture>
    <Furniture Name="name4">newValue2</Furniture>  
    <Furniture Name="name10">newValue5</Furniture>   
  </House>
  <House>
    <Brick Name="cubrick"></Brick>
    <Furniture name="name1>value</Furniture>
  </House>
</Street>

1 个答案:

答案 0 :(得分:0)

根据您的意见,我建议您考虑以下方法:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- MODIFY THESE AS NECESSARY -->
<xsl:param name="my-house">house1</xsl:param>

<xsl:variable name="my-values">
    <Furniture Name="name2">newValue2</Furniture>
    <Furniture Name="name4">newValue4</Furniture>
    <Furniture Name="name10">newValue10</Furniture>
</xsl:variable>

<!-- DO NOT MODIFY FROM THIS POINT ON -->
<xsl:variable name="user-values" select="exsl:node-set($my-values)/Furniture" />

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

<xsl:template match="House">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <xsl:if test="@Name=$my-house">
            <!-- add missing nodes -->
            <xsl:copy-of select="$user-values[not(@Name=current()/Furniture/@Name)]"/>
        </xsl:if>
    </xsl:copy>
</xsl:template>

<xsl:template match="Furniture">
    <xsl:variable name="override" select="$user-values[@Name=current()/@Name]"/>
    <Furniture Name="{@Name}">
        <xsl:choose>
            <xsl:when test="../@Name=$my-house and $override">
                <xsl:value-of select="$override" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="." />
            </xsl:otherwise>
        </xsl:choose>
    </Furniture>
</xsl:template>

</xsl:stylesheet>

当应用于以下格式良好的(!)示例输入时:

<强> XML

<Street>
  <House Name="house1">
    <Window>niceView</Window>
    <Furniture Name="name1">value1</Furniture>
    <Furniture Name="name2">value2</Furniture>
    <Furniture Name="name3">value3</Furniture>
    <Furniture Name="name4">value4</Furniture>     
  </House>
  <House>
    <Brick Name="cubrick"></Brick>
    <Furniture Name="name1">value</Furniture>
  </House>
</Street>

结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<Street>
   <House Name="house1">
      <Window>niceView</Window>
      <Furniture Name="name1">value1</Furniture>
      <Furniture Name="name2">newValue2</Furniture>
      <Furniture Name="name3">value3</Furniture>
      <Furniture Name="name4">newValue4</Furniture>
      <Furniture Name="name10">newValue10</Furniture>
   </House>
   <House>
      <Brick Name="cubrick"/>
      <Furniture Name="name1">value</Furniture>
   </House>
</Street>