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



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

我得到了房子的名字,以及应该在房子里的家具的名字和新值。

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

我对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>

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

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>

相关内容

  • 没有找到相关文章

最新更新