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
Xml xsl将标记分组到父项中_Xml_Xslt - Fatal编程技术网

Xml xsl将标记分组到父项中

Xml xsl将标记分组到父项中,xml,xslt,Xml,Xslt,我的xml是: <Row> <one>1</one> <two>2</two> <three>3</tree> <four>4</four> <... <... <... </Row> 我的预期结果是: <Row>

我的xml是:

 <Row>
        <one>1</one>
        <two>2</two>
        <three>3</tree>
        <four>4</four>
        <...
        <...
        <...
    </Row>
我的预期结果是:

<Row>
     <main>
            <value type="one_type">
              <stringValue>1</stringValue>
            </value>
            <value type="two_type">
              <stringValue>2</stringValue>
            </value>
      </main>

 <others>
        <three>3</tree>
        <four>4</four>
        <...
        <...
        <...
 <others>

</Row>
我的xsl是:

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


    <xsl:template match="one|two">
    <main>
        <xsl:choose>
            <xsl:when test="one">
                <value type="one_type">
                    <stringValue>
                        <xsl:apply-templates select="@*|node()"/>
                    </stringValue>
                </value> 
            </xsl:when>
            <xsl:otherwise>
                <value type="two_type">
                    <stringValue>
                        <xsl:apply-templates select="@*|node()"/>
                    </stringValue>
                </value> 
            </xsl:otherwise>
        </xsl:choose>
    </main>

    </xsl:template>
但这将返回两个不同的主标记,我希望所有标记都位于主标记下。
你知道怎么做吗?我原以为if-else会给我所需的结果,但事实并非如此。

您可以通过以下调整获得所需的输出

<?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" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/>
<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="Row">
    <main>
        <xsl:apply-templates select="node()|@*"/>
    </main>
</xsl:template>
<xsl:template match="one|two">
    <xsl:choose>
        <xsl:when test="local-name() = 'one'">
            <value type="one_type">
                <stringValue>
                    <xsl:apply-templates select="@*|node()"/>
                </stringValue>
            </value>
        </xsl:when>
        <xsl:otherwise>
            <value type="two_type">
                <stringValue>
                    <xsl:apply-templates select="@*|node()"/>
                </stringValue>
            </value>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>
输入:

<Row>
  <one>1</one>
  <two>2</two>
</Row>
<Row>
  <one>1</one>
  <two>2</two>
  <three>1</three>
  <four>2</four>
</Row>
<Row>
  <one>1</one>
  <two>2</two>
  <three>3</three>
  <four>4</four>
  <five>5</five>
  <six>6</six>
</Row>
输出:

<Row>
<main>
  <value type="one_type">
     <stringValue>1</stringValue>
  </value>
  <value type="two_type">
     <stringValue>2</stringValue>
  </value>
</main>
<three>1</three>
<four>2</four>
</Row>
<Row>
<main>
  <value type="one_type">
     <stringValue>1</stringValue>
  </value>
  <value type="two_type">
     <stringValue>2</stringValue>
  </value>
</main>
<three>3</three>
<four>4</four>
<five>5</five>
<six>6</six>
</Row>
OP第二次调整的更新:

作为6个条目的输入xml示例:

输入:

<Row>
  <one>1</one>
  <two>2</two>
</Row>
<Row>
  <one>1</one>
  <two>2</two>
  <three>1</three>
  <four>2</four>
</Row>
<Row>
  <one>1</one>
  <two>2</two>
  <three>3</three>
  <four>4</four>
  <five>5</five>
  <six>6</six>
</Row>
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" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/>
<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="Row">
<Row>
    <main>
        <xsl:apply-templates select="one | two "/>
    </main>
    <xsl:apply-templates select="node()[not(self::one | self::two)]"/>
</Row>
</xsl:template>
<xsl:template match="one|two">
    <xsl:choose>
        <xsl:when test="local-name() = 'one'">
            <value type="one_type">
                <stringValue>
                    <xsl:apply-templates select="@*|node()"/>
                </stringValue>
            </value>
        </xsl:when>
        <xsl:otherwise>
            <value type="two_type">
                <stringValue>
                    <xsl:apply-templates select="@*|node()"/>
                </stringValue>
            </value>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
<xsl:template match="node()">
   <xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
输出:

<Row>
<main>
  <value type="one_type">
     <stringValue>1</stringValue>
  </value>
  <value type="two_type">
     <stringValue>2</stringValue>
  </value>
</main>
<three>1</three>
<four>2</four>
</Row>
<Row>
<main>
  <value type="one_type">
     <stringValue>1</stringValue>
  </value>
  <value type="two_type">
     <stringValue>2</stringValue>
  </value>
</main>
<three>3</three>
<four>4</four>
<five>5</five>
<six>6</six>
</Row>

调整是将模板匹配行中的模板应用于除名为1和2的元素之外的所有节点,使用并更改仅复制主标记下的节点以匹配所有节点的模板-

我在该示例中未看到任何分组,您似乎希望将行转换为main,将行转换为value type=one\u类型,将行转换为value type=two\u类型

那么你从

<xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
好的,然后加上

<xsl:template match="Row">
  <main>
    <xsl:apply-templates select="@* | node()"/>
  </main>
</xsl:template>
然后

<xsl:template match="one | two">
  <value type="{local-name()}_type">
    <stringValue>
      <xsl:apply-templates/>
    </stringValue>
  </value>
</xsl:template>

对于已编辑的问题,请尝试:

<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="/Row">
    <xsl:copy>
        <main>
            <xsl:apply-templates select="one|two"/>
        </main>
        <xsl:apply-templates select="three|four"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="one|two">
    <value type="{local-name()}_type">
        <stringValue>
            <xsl:value-of select="."/>
        </stringValue>
    </value>
</xsl:template>

</xsl:stylesheet>
如果您不确定除了和可以显示哪些元素,请更改此行:

<xsl:apply-templates select="three|four"/>
致:


如果您真的想在模板内进行测试,那么您可以test=self::one。但通常写一个匹配模式就足够了。我已经改变了一个问题,你能看到变化吗?我需要一个两个标签在单独的主标签内行标签。你能编辑你的答案吗?@user3379892很高兴我能帮助你。如果这回答了你的问题,你可以考虑接受这个答案或其他答案之一,所以问题将被标记为关闭。如果您不知道这是如何工作的,只需查看谢谢您的帮助。我再次对我的问题进行了编辑,使其更具一般性。我需要所有不是一和二的其他标签,放入其他标签中。@user3379892刚刚更新了我对更新问题的答案。太好了!这很有帮助