1

我需要一些关于 XSLT 如何在 XSLT 中执行以下操作的说明。

我有这个源文件。

<Data>
    <additem>                   
                <choice>desc</choice>
                <sectiontext>
                    <a title="google" href="http://google.com" xmlns="http://www.w3.org/1999/xhtml">
                        <strong>Sample Text</strong>
                        <ul>
                            <li><em>aa</em></li>
                            <li><em>bb</em></li>
                            <li><em>cc</em></li>
                        </ul>
                    </a>                        
                </sectiontext>

    </additem>
    <additem>   
                <choice>image</choice>
                <files>
                    <a xmlns="http://www.w3.org/1999/xhtml" title="image location" href="xyz:12-2022">
                        <img  alt="No Image" title="No Image" xlink:href="some image path" xmlns:xlink="http://www.w3.org/1999/xlink"></img>
                    </a>
                </files>
    </additem>
            <additem>                   
                <choice>Paragraph</choice>
                <sectiontext>
                    <a title="google" href="http://google.com" xmlns="http://www.w3.org/1999/xhtml">
                        <strong>Sample Text</strong>
                        <ul>
                            <li><em>aa</em></li>
                            <li><em>bb</em></li>
                            <li><em>cc</em></li>
                        </ul>
                    </a>
                    hello alll

                </sectiontext>

    </additem>
</Data>

输出:

<Information>

        <Section>
            <text>
                <strong>Sample Text</strong>
                        <ul>
                            <li><em>aa</em></li>
                            <li><em>bb</em></li>
                            <li><em>cc</em></li>
                        </ul>
            </text>
            <link external="http://google.com" title="google"></link>
        </Section>
        <picture>
            <image src="some image path" altText="No Image">
                <link local="xyz:12-2022" title="image location"></link>
            </image>
        </picture>
        <Body>
            <text>
                <hyperlink>
                    <text>
                        <strong>Sample Text</strong>
                                <ul>
                                    <li><em>aa</em></li>
                                    <li><em>bb</em></li>
                                    <li><em>cc</em></li>
                                </ul>
                    </text> 
                    <link external="http://google.com" title="google"></link>
                </hyperlink>
                hello alll
            </text>
        </Body>
</Information>

规则:

1.根据addItem/choice中的选择,我们需要创建标签。

    choice    --    Desc
    desc    --      Section
    image   --      picture
    Paragraph----Body

2.处理标签当前标签正在包装其他标签。

    A.If any element has only <a> in it. For example in the source,

            Code in the source:
            <sectiontext>
                    <a title="google" href="http://google.com" xmlns="http://www.w3.org/1999/xhtml">
                        <strong>Sample Text</strong>
                        <ul>
                            <li><em>aa</em></li>
                            <li><em>bb</em></li>
                            <li><em>cc</em></li>
                        </ul>
                    </a>                        
            </sectiontext>

需要分离该标签并创建一个标签

i. if the "href" in attribute in <a> tag starts with "xyz:" need to add it as "local" attribute in <link> element
ii. If the "href" in the attribute <a> tag starts with "http" need to add it as "external" attribute in <link> element.
ii. "title" attribute in <a> tag remains same in <link>

    B.if any element has any other element other than <a> tag.

        Code in the source:

        <sectiontext>
                    <a title="google" href="http://google.com" xmlns="http://www.w3.org/1999/xhtml">
                        <strong>Sample Text</strong>
                        <ul>
                            <li><em>aa</em></li>
                            <li><em>bb</em></li>
                            <li><em>cc</em></li>
                        </ul>
                    </a>
                    hello alll

        </sectiontext>

我需要把输出作为

        <text>
                <hyperlink>
                    <text>
                        <strong>Sample Text</strong>
                                <ul>
                                    <li><em>aa</em></li>
                                    <li><em>bb</em></li>
                                    <li><em>cc</em></li>
                                </ul>
                    </text> 
                    <link external="http://google.com" title="google"></link>
                </hyperlink>
                hello alll
        </text>

规则:

  i. In the all the text inside the <a> tag have to come under the <inlinelink> tag as shown above.

任何人都可以帮助它如何完成。

谢谢你。

4

1 回答 1

0

这个 XSLT 1.0 样式表...

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:xhtml="http://www.w3.org/1999/xhtml"
                              xmlns:xlink="http://www.w3.org/1999/xlink" >
<xsl:output method="xml" indent="yes"/>

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

<xsl:template match="/">
 <Information>
   <xsl:apply-templates select="Data/additem"/>
 </Information>
</xsl:template>      

<xsl:template match="xhtml:a[../../self::additem]">
 <link title="{@title}">
  <xsl:if test="starts-with(@href,'http')">
    <xsl:attribute name="external"><xsl:value-of select="@href" /></xsl:attribute>
  </xsl:if>
  <xsl:if test="starts-with(@href,'xyz:')">
    <xsl:attribute name="local"><xsl:value-of select="@href" /></xsl:attribute>
  </xsl:if>
 </link>
</xsl:template>      

<xsl:template match="additem[choice='desc']">
 <Section>
  <text>
    <xsl:apply-templates select="sectiontext/xhtml:a/*" /> 
  </text>
  <xsl:apply-templates select="sectiontext/xhtml:a" />
 </Section>
</xsl:template>      

<xsl:template match="additem[choice='image']">
 <picture>
  <image src="{files/xhtml:a/xhtml:img/@xlink:href}" altText="{files/xhtml:a/xhtml:img/@alt}">
   <apply-templates select="files/xhtml:a" />
  </image>
 </picture>
</xsl:template>      

<xsl:template match="additem[choice='Paragraph']">
 <Body>
  <text>
   <hyperlink>
    <text>
     <xsl:apply-templates select="sectiontext/xhtml:a/*" />
    </text> 
    <xsl:apply-templates select="sectiontext/xhtml:a" />
   </hyperlink>
   <xsl:apply-templates select="sectiontext/node()[not(self::xhtml:a)]" />
  </text>
 </Body>
</xsl:template>      

</xsl:stylesheet>

... 将您指定的输入文档转换为此输出文档...

<?xml version="1.0" encoding="utf-8"?>
<Information xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink">
  <Section>
    <text>
      <strong xmlns="http://www.w3.org/1999/xhtml">Sample Text</strong>
      <ul xmlns="http://www.w3.org/1999/xhtml">
                            <li><em>aa</em></li>
                            <li><em>bb</em></li>
                            <li><em>cc</em></li>
                        </ul>
    </text>
    <link title="google" external="http://google.com" />
  </Section>
  <picture>
    <image src="some image path" altText="No Image">
      <apply-templates select="files/xhtml:a" />
    </image>
  </picture>
  <Body>
    <text>
      <hyperlink>
        <text>
          <strong xmlns="http://www.w3.org/1999/xhtml">Sample Text</strong>
          <ul xmlns="http://www.w3.org/1999/xhtml">
                            <li><em>aa</em></li>
                            <li><em>bb</em></li>
                            <li><em>cc</em></li>
                        </ul>
        </text>
        <link title="google" external="http://google.com" />
      </hyperlink>

                    hello alll

                </text>
  </Body>
</Information>

解释

您的每条规则都被一一采用并用于构建模板,从识别匹配条件开始。

于 2012-06-21T15:14:48.200 回答