Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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
是否仅在XSLT1.0中复制直接子级及其属性?_Xslt_Xslt 1.0 - Fatal编程技术网

是否仅在XSLT1.0中复制直接子级及其属性?

是否仅在XSLT1.0中复制直接子级及其属性?,xslt,xslt-1.0,Xslt,Xslt 1.0,输入XML: <root> <recordList> <record priref="1"> <Group attr="val"> <Field1>Value X</Field1> <Field2> <value lang="en-US">Foo</value> <value lang="

输入XML:

<root>
  <recordList>
    <record priref="1">
      <Group attr="val">
        <Field1>Value X</Field1>
        <Field2>
          <value lang="en-US">Foo</value>
          <value lang="de-DE">Bar</value>
        </Field2>
      </Group>
      <Field3 attr="val">Value Y</Field3>
    </record>
    <record priref="2">
      <Field3 attr="val">Value Z</Field3>
    </record>
  </recordList>
</root>

值X
福
酒吧
Y值
值Z
所需的输出(类似于仅包含直接子元素和属性的“浅拷贝”):


Y值
值Z
是否有其他方法(例如,每个都没有)来实现这一点

<xsl:template match="/">
  <root>
    <xsl:apply-templates select="root/recordList/record" />
  </root>
</xsl:template>

<xsl:template match="record">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:for-each select="*">
      <xsl:copy>
        <xsl:copy-of select="@* | text()"/>
      </xsl:copy>
    </xsl:for-each>
  </xsl:copy>
</xsl:template>


编辑:文本节点“值Y”和“值Z”实际上应该在结果中。“Foo”和“Bar”在结果中的任何地方都是不需要的。

非常短的模板使用
父轴::
-axis:

<xsl:template match="root|record|*[parent::record]|*[parent::record]/text()|@*">
  <xsl:copy>
    <xsl:apply-templates select="node()|@*" />
  </xsl:copy>
</xsl:template>

<xsl:template match="text()" />

它的输出是

<?xml version="1.0"?>
<root>
  <record priref="1">
    <Group attr="val">
    </Group>
  <Field3 attr="val">Value Y</Field3></record>
  <record priref="2">
    <Field3 attr="val">Value Z</Field3>
  </record>
</root>

Y值
值Z

非常短的模板使用
父轴::
-axis:

<xsl:template match="root|record|*[parent::record]|*[parent::record]/text()|@*">
  <xsl:copy>
    <xsl:apply-templates select="node()|@*" />
  </xsl:copy>
</xsl:template>

<xsl:template match="text()" />

它的输出是

<?xml version="1.0"?>
<root>
  <record priref="1">
    <Group attr="val">
    </Group>
  <Field3 attr="val">Value Y</Field3></record>
  <record priref="2">
    <Field3 attr="val">Value Z</Field3>
  </record>
</root>

Y值
值Z

一种方法是不处理任何孙辈:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

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

    <xsl:template match="recordList">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="record/*/node()"/>
</xsl:transform>

一种方法是不处理任何孙辈:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

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

    <xsl:template match="recordList">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="record/*/node()"/>
</xsl:transform>

干脆不将模板应用于您不想要的节点怎么样

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="recordList">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="record/*">
    <xsl:copy>
        <xsl:apply-templates select="@*|text()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

干脆不将模板应用于您不想要的节点怎么样

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="recordList">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="record/*">
    <xsl:copy>
        <xsl:apply-templates select="@*|text()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>


很抱歉,所需输出中有错误(请参阅我编辑的问题)。对于您的解决方案,结果是
值XFooBar
(所有文本节点连接)和
(无文本节点),但我要查找的是
(空节点,无文本子节点)和
值Y
(带文本子节点)。很抱歉,所需输出中有错误(请参阅我编辑的问题)。使用您的解决方案,结果是
值XFooBar
(所有文本节点连接)和
(无文本节点),但我要查找的是
(空节点,无文本子节点)和
值Y
(带文本子节点)。考虑得好。如果我将最后一个模板调整为
record/*/*
,结果看起来就像我(更新的)所需的输出!与
一起,它是完美的(我想排除recordList附近的元素,包括它们的文本节点)。谢谢我首先使用了
记录/*/*
,但随后将其更改为
记录/*/node()
,因为您在某个阶段请求的输出似乎有空的
字段3
。但是方法是重要的一步。好的想法。如果我将最后一个模板调整为
record/*/*
,结果看起来就像我(更新的)所需的输出!与
一起,它是完美的(我想排除recordList附近的元素,包括它们的文本节点)。谢谢我首先使用了
记录/*/*
,但随后将其更改为
记录/*/node()
,因为您在某个阶段请求的输出似乎有空的
字段3
。但这种方法是重要的一步。被接受,因为它看起来是所有方法中最干净的。我删除了记录列表模板,这里不需要。@CoDEmanX该模板是获得问题中显示的输出所必需的。你是对的,我没有注意到在没有该模板的输出中存在
元素。接受该模板,因为它看起来是最干净的方法。我删除了此处不需要的记录列表模板。@CoDEmanX该模板是获取问题中显示的输出所必需的。你是对的,我没有注意到在没有该模板的输出中存在
元素。