Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 当任意存在同级元素时,XSLT是否按顺序创建元素?_Xml_Xslt - Fatal编程技术网

Xml 当任意存在同级元素时,XSLT是否按顺序创建元素?

Xml 当任意存在同级元素时,XSLT是否按顺序创建元素?,xml,xslt,Xml,Xslt,假设您有一个具有内容模型的元素,如下所示: 换句话说,在包装器元素中,有一个指定顺序的子元素,这些子元素可以任意存在 您需要在包装器中创建一个新元素(例如m),同时保留已有的内容,并确保输出符合内容模型 这是一种解决方案: <xsl:template match="@*|node()"> <xsl:sequence select="."/> </xsl:template> <xsl:template match="wrapper">

假设您有一个具有内容模型的元素,如下所示:


换句话说,在包装器元素中,有一个指定顺序的子元素,这些子元素可以任意存在

您需要在包装器中创建一个新元素(例如m),同时保留已有的内容,并确保输出符合内容模型

这是一种解决方案:

<xsl:template match="@*|node()">
  <xsl:sequence select="."/>
</xsl:template>

<xsl:template match="wrapper">
  <xsl:copy>
    <xsl:apply-templates select="a,b,c,d,e,f,g,h,i,j,k,l,m"/>
    <m>This is new</m>
    <xsl:apply-templates select="n,o,p,q,r,s,t,u,v,w,x,y,z"/>
  </xsl:copy>
</xsl:template>

这是新的
但是,此解决方案将删除包装器元素中的所有空白、注释或处理指令。我已经想出了一些解决方案,这些解决方案不会放弃这些东西,但没有一个是我满意的

这个问题最优雅的解决方案是什么,它不会删除节点?XSLT 3和支持模式的解决方案很好

以下是一些输入和输出示例:

<!-- Input -->
<wrapper/>

<!-- Output -->
<wrapper><m>This is new</m></wrapper>

<!-- Input -->
<wrapper><a/><z/></wrapper>

<!-- Output -->
<wrapper><a/><m>This is new</m><z/></wrapper>

<!-- Input -->
<wrapper><m/></wrapper>

<!-- Output -->
<wrapper><m/><m>This is new</m></wrapper>

<!-- Input -->
<wrapper>
  <a/>
  <!-- put m here -->
  <z/>
</wrapper>

<!-- Output -->
<!-- NB: The comment and whitespace could come after the inserted element instead. This case is ambiguous -->
<wrapper>
  <a/>
  <!-- put m here --><m>This is new</m>
  <z/>
</wrapper>

<!-- Input -->
<wrapper>
  <a/>
  <b/>
  <c/>
  <n/>
  <o/>
  <!-- p is not here -->
  <?do not drop this?>
</wrapper>

<!-- Output -->
<wrapper>
  <a/>
  <b/>
  <c/><m>This is new</m>
  <n/>
  <o/>
  <!-- p is not here -->
  <?do not drop this?>
</wrapper>

这是新的
这是新的
这是新的
这是新的
这是新的

插入元素周围的非元素节点在插入元素之前或之后并不重要,只是它们不会被删除,并且它们相对于原始元素的顺序会保留下来。

这里有一种方法可以查看它:

XSLT2.0

<xsl:stylesheet version="2.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="wrapper">
    <xsl:variable name="all" select="node()"/>
    <xsl:variable name="middle" select="(n,o,p,q,r,s,t,u,v,w,x,y,z)[1]"/>
    <xsl:variable name="i" select="if($middle) then count($all[. &lt;&lt; $middle]) else count($all)"/>
    <xsl:variable name="new">
        <m>This is new</m>
    </xsl:variable>
    <xsl:copy>
        <xsl:apply-templates select="insert-before($all, $i+1, $new) "/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

这是新的

或者,如果您愿意:

<xsl:template match="wrapper">
    <xsl:variable name="all" select="node()"/>
    <xsl:variable name="middle" select="(a,b,c,d,e,f,g,h,i,j,k,l,m)[last()]"/>
    <xsl:variable name="i" select="if($middle) then index-of($all/generate-id(), generate-id($middle)) else 0"/>
    <xsl:variable name="new">
        <m>This is new</m>
    </xsl:variable>
    <xsl:copy>
        <xsl:apply-templates select="insert-before($all, $i+1, $new) "/>
    </xsl:copy> 
</xsl:template>

这是新的

您可以指示与子元素匹配的模板复制任何尾随(或前导)文本、注释或处理指令节点。-P.S.A在这里可能有用。@Danielhaley
m
据我所知可能不存在