在同名的xml元素之间添加xsl的空格,一些嵌套

时间:2016-09-29 00:27:25

标签: xml xslt xslt-1.0

给出以下xml:

<?xml version="1.0" encoding="UTF-8"?>
<us-patent-grant lang="EN" >
<us-claim-statement>What is claimed is:</us-claim-statement>
<claims id="claims">
<claim id="CLM-00001" num="00001">
<claim-text>1. A computer-implemented method, comprising:
<claim-text>receiving data comprising:
<claim-text>image data depicting in the user device; and</claim-text>
<claim-text>motion data in the user device;</claim-text>
</claim-text>
<claim-text>determining of the user;</claim-text>
<claim-text>determining motion data;</claim-text>
<claim-text>processing the user; and</claim-text>
<claim-text>based on the user.</claim-text>
</claim-text>
</claim>
<claim id="CLM-00002" num="00002">
<claim-text>2. The method of <claim-ref idref="CLM-00001">claim 1</claim-ref>, wherein the comprises:
<claim-text>identifying in the image data;</claim-text>
<claim-text>retrieving  object;</claim-text>
<claim-text>based on the reference object; and</claim-text>
<claim-text>determining  the proportion.</claim-text>
</claim-text>
</claim>
</claims>
</us-patent-grant>

和这个xsl:

<?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" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="us-patent-grant | us-patent-application">
    <xsl:element name="patent-info">


        <xsl:element name="claim1TextAttempt1">         
            <xsl:for-each select="claims/claim[@id='CLM-00001']/claim-text">
                <xsl:value-of select="concat(' ', .)"/>
            </xsl:for-each>         
        </xsl:element>

        <xsl:element name="claim1TextAttempt2">         
            <xsl:for-each select="claims/claim[@id='CLM-00001']">
                <xsl:value-of select="claim-text" />
                <xsl:if test="position()!=last()">
                    <xsl:text>, </xsl:text>
                </xsl:if>   
            </xsl:for-each>         
        </xsl:element>

    </xsl:element>
</xsl:template>
</xsl:stylesheet>

我得到这样的结果:

<claim1Text>1. A computer-implemented method, comprising:
receiving data comprising:
image data depicting in the user device; andmotion data in the user device;determining of the user;determining motion data;processing the user; andbased on the user.</claim1Text>

两次尝试......

&#34; andmotion&#34;和&#34;和#34;连接,而不是单独的单词。我希望这些是分开的。我可以删除:

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

然后它看起来很乱。我可以在xsl中添加空格来做些什么吗?我正在使用xsl 1.0。非常感谢你!

编辑 - 预期结果(&#39;以及&#39;和&#39;以及&#39;和#39;)中的空格:

<?xml version="1.0" encoding="UTF-8"?>
<reed-patents>
   <patent-info>
      <claim1Text>1. A computer-implemented method, comprising:
receiving data comprising:
image data depicting in the user device; and motion data in the user device;determining of the user;determining motion data;processing the user; and based on the user.</claim1Text>

   </patent-info>
</reed-patents>

1 个答案:

答案 0 :(得分:1)

如下:

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/*">
    <reed-patents>
        <patent-info>
            <xsl:for-each select="claims/claim">
                <claimText>
                    <xsl:apply-templates/>
                </claimText>
            </xsl:for-each>
        </patent-info>
    </reed-patents>
</xsl:template>

<xsl:template match="claim-text/text()">
    <xsl:value-of select="."/>
    <xsl:variable name="last-char" select="substring(., string-length())" />
    <xsl:if test="normalize-space($last-char)">
        <xsl:text> </xsl:text>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

应用于您的示例输入,结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<reed-patents>
   <patent-info>
      <claimText>1. A computer-implemented method, comprising:
receiving data comprising:
image data depicting in the user device; and motion data in the user device; determining of the user; determining motion data; processing the user; and based on the user. </claimText>
      <claimText>2. The method of claim 1, wherein the comprises:
identifying in the image data; retrieving object; based on the reference object; and determining the proportion. </claimText>
   </patent-info>
</reed-patents>

加了:

要仅从第一个声明中获取文字,请制作第一个模板:

<xsl:template match="/*">
    <reed-patents>
        <patent-info>
            <claimText>
                <xsl:apply-templates select="claims/claim[@id='CLM-00001']" />
            </claimText>
        </patent-info>
    </reed-patents>
</xsl:template>