xml - 如何检查元素是否存在并选择元素值的子字符串

标签 xml xslt xslt-1.0 addition

我有一个 XML 文件:

<?xml version="1.0" encoding="windows-1250"?>
<CONTACTS>
    <CONTACT>
        <FirstName>AfgZohal</FirstName>
        <LastName>Zohal Afg</LastName>
    </CONTACT>
    <CONTACT>
        <FirstName>Arun_niit</FirstName>
        <LastName>Arun_niit</LastName>
        <EMail><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="523c2720330d3b3137122b333a3d3d7c313d7c3b3c" rel="noreferrer noopener nofollow">[email protected]</a></EMail>
    </CONTACT>
    <CONTACT>
        <FirstName>Bống M&#xc5;©nHá&#xba;&#xa3;i</FirstName>
        <LastName>Há&#xba;&#xa3;i Anh Bống M&#xc5;©n</LastName>
        <URL>http://www.facebook.com/profile.php?id=100000689849077</URL>
    </CONTACT>
</CONTACTS>

我想在我的 xml 文件中的 FirstName 之前添加一个元素 ID;如果 URL 可用,我想从 URL 标记中提取 ID,或者我想从电子邮件地址中提取前六个字母以将其放入 ID 中(唯一) )。因为在有些联系人中,没有URL。我为此使用 XSLT。 在我的 XSl 文件中,我尝试了这种方式

   <ID>
     <xsl:value-of select="CONTACT/URL[//http='@id']"/>
     </ID>

但它不起作用,这是我的 XSL 文件:

<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="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="CONTACT">
        <xsl:copy>
         <ID>
         <xsl:value-of select="CONTACT/URL[//http='@id']"/>
         </ID>
              <xsl:copy-of select="FirstName|LastName|URL"/>
            <EMAILS>
                <xsl:apply-templates select="EMail"/>
            </EMAILS>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="EMail">
        <EMail>
            <Type><xsl:value-of select="substring-before(
                    substring-after(.,'@'),
                    '.')"/>
            </Type>
            <Value><xsl:value-of select="."/></Value>
        </EMail>
    </xsl:template>

</xsl:stylesheet>

这是我的输出 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<CONTACTS>
<CONTACT>
<ID/>
<FirstName>AfgZohal</FirstName>
<LastName>Zohal Afg</LastName>
<EMAILS/>
</CONTACT>
<CONTACT>
<ID/>
<FirstName>Arun_niit</FirstName>
<LastName>Arun_niit</LastName>
<EMAILS>
<EMail>
<Type>yahoo</Type>
<Value><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e302b2c3f01373d3b1e273f363131703d31703730" rel="noreferrer noopener nofollow">[email protected]</a></Value>
</EMail>
</EMAILS>
</CONTACT>
<CONTACT>
<ID/>
<FirstName>Bống MũnHải</FirstName>
<LastName>Hải Anh Bống Mũn</LastName>
<URL>http://www.facebook.com/profile.php?id=100000689849077</URL>
<EMAILS/>
</CONTACT>
<CONTACT>
</CONTACTS>

这是我昨天的问题的一部分 Novice transformation using apply-templates and string manipulation in the child node ;因为这是一个不同的问题,所以我提出了一个不同的问题。

最佳答案

您似乎想要选择 URL 元素内 url 字符串的“id”部分。您应该选择 ?id= 之后的子字符串:

 <ID>
 <xsl:value-of select="substring-after(URL,'?id=')"/>
 </ID>

此外,在模板中,您处于 CONTACT 的上下文中,因此要选择它的子元素,您只需指定元素的名称即可。示例:

 <xsl:template match="CONTACT">
         <xsl:value-of select="URL"/>
  </xsl:template>

将返回 URL 的值,同时:

 <xsl:template match="CONTACT">
         <xsl:value-of select="CONTACT/URL"/>
  </xsl:template>

不会返回任何内容,没有 CONTACT 任何 CONTACT/URL 类型的子项。


评论问题的奖励答案:

I would like to extract ID from URL tag if URL is available or I want to extract the first six letters from Email address to put it in the ID(unique) (...) If we have one/multiple email address then we can select any one of the first six letters from the email address. I thing the contacts must have at least one email address if there is no URL...

        <ID>
            <xsl:choose>
                <xsl:when test="URL">
                    <xsl:value-of select="substring-after(URL,'?id=')"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="substring-before(EMail[1],'@')"/>
                </xsl:otherwise>
            </xsl:choose>
        </ID>

关于xml - 如何检查元素是否存在并选择元素值的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6400884/

相关文章:

java - Android XML 中的多个错误

xml - 根据名称、属性名称、属性值和节点值对 XML 进行排序

android - 错误 :(27) No resource identifier found for attribute 'srcCompat' in package 'com.example.jaisonjoseph.newsclient'

java - 如何更改 xml 审计文件中的时区(Oracle 11g)?

xml - XSLT替换变量中的双引号

javax.xml.transform.Transformer 输入源大小限制

xml - 基于nodeName的xpath选择

.net - 使用 .net 进行 xslt 转换的缩进和文本节点

xml - 如何检查xml列表中的两个代码?

java - XSLT 1 将 ID 分组并组合到 csv