Xml XSLT-如何统一某些后续节点的文本内容?

Xml XSLT-如何统一某些后续节点的文本内容?,xml,xaml,xslt,Xml,Xaml,Xslt,我对XSLT非常陌生。我正在将自制的标记语言转换为XAML。由于历史原因,输入XML中的文本组件使用单个节点表示,即使它们是同一段落的一部分 编辑 段落实际上是由一个或它后面的另一个非文本节点确定的。非文本节点是应单独呈现的节点,而不是段落的一部分,如标题,例如;为了简单起见,我们可以假设所有要统一的节点都有一个“text-”前缀,例如,等 (在下面的示例中,输出是伪代码XAML,因为我不是一个有经验的XAML开发人员。) 例1 你好,世界!富吧 第二行 上面的例子应该会产生如下结果 <

我对XSLT非常陌生。我正在将自制的标记语言转换为XAML。由于历史原因,输入XML中的文本组件使用单个节点表示,即使它们是同一段落的一部分

编辑

段落实际上是由一个

或它后面的另一个非文本节点确定的。非文本节点是应单独呈现的节点,而不是段落的一部分,如标题,例如
;为了简单起见,我们可以假设所有要统一的节点都有一个“text-”前缀,例如

(在下面的示例中,输出是伪代码XAML,因为我不是一个有经验的XAML开发人员。)

例1


你好,世界!富吧
第二行
上面的例子应该会产生如下结果

<output>
  <TextBlock>Hello World!<bold>foo bar</bold></TextBlock>
  <TextBlock>Second line</TextBlock>
</output>

你好,世界!富吧
第二线
例2


你好,世界!富吧
打断段落的标题
应该让步

<output>
  <TextBlock>Hello World!<bold>foo bar</bold></TextBlock>
  <TextBlock><bold>A headline breaking a paragraph</bold></TextBlock>
</output>

你好,世界!富吧
打断段落的标题
我正在考虑使用
以下同级::
和/或
前面的同级::
,但是,当然,可以组合任意数量的节点。如何做到这一点?

使用此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="no"/>

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

    <xsl:template match="text-plain">
      <xsl:value-of select="." />
    </xsl:template>

    <xsl:template match="text-bold">
      <bold>
        <xsl:value-of select="." />
      </bold>
    </xsl:template>

    <xsl:template match="br">
      <xsl:copy-of select="." />
    </xsl:template>

</xsl:stylesheet>

使用此输入(我添加了一个根节点)


你好,世界!
富吧
产生您想要的输出:

<?xml version="1.0"?>
<TextBlock>
  Hello World!
  <bold>foo bar</bold><br/>
</TextBlock>

你好,世界!
富吧

好的,就像Michael建议的,这是一个分组问题。我已经能够通过对输入进行两次传递来解决这个问题:第一次传递为所有text-*节点添加一个属性,第二次传递使用该属性来标识所有相邻节点,这些节点应在一个TextBlock节点下分组

<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:template match="/">
    <!-- store the modified content in a variable -->
    <xsl:variable name="preprocessed.doc">
        <xsl:apply-templates mode="preprocess" />
    </xsl:variable>

    <!-- process the modified contents -->
    <xsl:apply-templates select="$preprocessed.doc/*" />
</xsl:template>


<!-- Preprocessing: add type attribute to all text-* nodes -->

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

<xsl:template match="*[name()[matches(.,'text-.*')]]" mode="preprocess">
  <xsl:copy>
    <xsl:attribute name="type">text</xsl:attribute>
    <xsl:apply-templates select="@*|node()" mode="preprocess"/>
  </xsl:copy>
</xsl:template>


<!-- Main processing: group text-* nodes, transform text-* nodes, etc. -->

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

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

<xsl:template match="@type">
    <!-- Delete the type attributes from the final result -->
</xsl:template>

<xsl:template match="input">
    <output>
        <xsl:for-each-group select="*" group-adjacent="@type='text'">
            <xsl:choose>
                <xsl:when test="name()[matches(.,'text-.*')]">
                    <TextBlock>
                        <xsl:apply-templates select="current-group()"/>
                    </TextBlock>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates select="current-group()"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each-group>
    </output>
</xsl:template>

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

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

</xsl:stylesheet>

文本

我相信,如果您可以使用XSLT 2.0,您可以通过执行以下操作(相对)使其保持简单:

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="/input">
    <output>
        <xsl:for-each-group select="*" group-adjacent="starts-with(name(), 'text-')">
            <xsl:choose>
                <xsl:when test="starts-with(name(), 'text-')">
                    <TextBlock>
                        <xsl:apply-templates select="current-group()"/>
                    </TextBlock>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates select="current-group()"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each-group>
    </output>
</xsl:template>

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

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

<xsl:template match="br"/>

</xsl:stylesheet>

应用于以下测试输入:

XML

<input>
    <h1>Main Headline</h1>
    <text-plain>First line </text-plain>
    <text-bold>emphasises</text-bold>
    <text-plain> the first end.</text-plain>
    <br/>
    <text-plain>Second line.</text-plain>
    <h2>Aux Headline Breaking a Paragraph</h2>
    <text-plain>Third line </text-plain>
    <text-bold>boldens</text-bold>
    <text-plain> the third end.</text-plain>
</input>

主要标题
一线
强调
第一头。

第二行。 打破段落的标题 第三行 博尔登 第三端。
结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <h1>Main Headline</h1>
   <TextBlock>First line <bold>emphasises</bold> the first end.</TextBlock>
   <TextBlock>Second line.</TextBlock>
   <h2>Aux Headline Breaking a Paragraph</h2>
   <TextBlock>Third line <bold>boldens</bold> the third end.</TextBlock>
</output>

主要标题
第一行强调第一个端点。
第二行。
打破段落的标题
第三行加粗了第三个端点。

我们如何知道它们是同一段落的一部分?请发布一个更完整的示例,最好在其父元素中显示至少两段。-看,这基本上是一个分组问题。您能使用XSLT2.0吗?另外,什么是“非文本节点”?@michael.hor257k我必须询问XSLT2.0。谢谢。但这会使我的第二个示例在一个TextBlock节点中包含所有内容,这不是我想要的。@Florian:你完全改变了你的问题。所以一个令人满意的答案也会是完全不同的。我不明白你为什么需要两次传球。我没有想到一开始就是-太棒了!非常感谢你的帮助!它与
匹配项(name(),'text-.*)
一样有效。
<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="/input">
    <output>
        <xsl:for-each-group select="*" group-adjacent="starts-with(name(), 'text-')">
            <xsl:choose>
                <xsl:when test="starts-with(name(), 'text-')">
                    <TextBlock>
                        <xsl:apply-templates select="current-group()"/>
                    </TextBlock>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates select="current-group()"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each-group>
    </output>
</xsl:template>

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

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

<xsl:template match="br"/>

</xsl:stylesheet>
<input>
    <h1>Main Headline</h1>
    <text-plain>First line </text-plain>
    <text-bold>emphasises</text-bold>
    <text-plain> the first end.</text-plain>
    <br/>
    <text-plain>Second line.</text-plain>
    <h2>Aux Headline Breaking a Paragraph</h2>
    <text-plain>Third line </text-plain>
    <text-bold>boldens</text-bold>
    <text-plain> the third end.</text-plain>
</input>
<?xml version="1.0" encoding="UTF-8"?>
<output>
   <h1>Main Headline</h1>
   <TextBlock>First line <bold>emphasises</bold> the first end.</TextBlock>
   <TextBlock>Second line.</TextBlock>
   <h2>Aux Headline Breaking a Paragraph</h2>
   <TextBlock>Third line <bold>boldens</bold> the third end.</TextBlock>
</output>