xpath - xslt 1.0 用同一 xml 中的实际数据替换 xpath 引用

标签 xpath reference replace xslt-1.0

我有一些要求,我需要替换同一个 xml 文件中的引用。
限制是仅使用 xslt 1.0。

下面是我的示例输入 xml。

    <org>
       <depts>
          <dept>
             <deptId>1009</deptId>
                <deptName>IT</deptName>
                <deptAccessCode>IT-1009</deptAccessCode>
          </dept>
          <dept>
             <deptId>2344</deptId>
             <deptName>BPO</deptName>
             <deptAccessCode>BP-2344</deptAccessCode>
          </dept>
          </depts>
          <employees>
             <employee>
             <name>abc</name>
             <dept>
              <REFERENCE>
                 <LocationXPath>/org/depts/dept[2]</LocationXPath>
              </REFERENCE>
          </dept>
          <employee>
       </employees>
</org>

现在我想用 XPath/org/depts/dept[2] 中的实际数据替换节点 REFERENCE。
所以输出 xml 应该如下所示。
    <org>
       <depts>
          <dept>
             <deptId>1009</deptId>
                <deptName>IT</deptName>
                <deptAccessCode>IT-1009</deptAccessCode>
          </dept>
          <dept>
             <deptId>2344</deptId>
             <deptName>BPO</deptName>
             <deptAccessCode>BP-2344</deptAccessCode>
          </dept>
          </depts>
          <employees>
             <employee>
             <name>abc</name>
             <dept>
             <deptId>2344</deptId>
             <deptName>BPO</deptName>
             <deptAccessCode>BP-2344</deptAccessCode>
          </dept>
          <employee>
       </employees>
    </org>

我在不同的元素中有几个 REFERENCE 节点,它们引用了 xml 树中的不同 xpath,我需要用实际数据替换它们。
<someWhereInTheXmlTree>
<sometag>
   <REFERENCE>
      <LocationXPath>some/reference[1]/to/a/node[3]/in/the[4]/same/xml</LocationXPath>
   </REFERENCE>
</sometag>
<someWhereInTheXmlTree>
...
<ffff>
<bbbb>
   <REFERENCE>
      <LocationXPath>abc/xyz[1]/node[4]/element</LocationXPath>
   </REFERENCE>
</bbbb>
<ffff>

请帮助我。
在此先感谢您的帮助。

到目前为止,我已经实现了一个 XSLT 来替换引用,但现在我面临着不需要的空 namespace 。

这是我的 XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tib="http://www.tibco.com/bw/xslt/custom-functions"
xmlns="http://www.realestate.org/residential/2010/schemas" >

<xsl:output omit-xml-declaration="no" indent="yes" method = "xml" />
<xsl:strip-space elements="*"/>
<xsl:param name="myxml"  />
<xsl:template match="node()|@*">
    <xsl:param name="isNodeToReplace"><xsl:call-template name="ReferenceCheck" /></xsl:param>
    <xsl:choose>
        <xsl:when test="$isNodeToReplace='true'">
            <xsl:call-template name="replaceWithData">
                <xsl:with-param name="ref"><xsl:value-of select="." /></xsl:with-param>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="ReferenceCheck">
    <xsl:choose>
        <xsl:when test="name(child::*[1])='REFERENCE' and name(child::*[1]//child::*[1])='LocationXPath'">true</xsl:when>
        <xsl:otherwise>false</xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="replaceWithData">
    <xsl:param name="ref" />
    <xsl:copy-of select="tib:evaluate($myxml,$ref)" />
</xsl:template>

</xsl:stylesheet>

在上面的 XSLT 中,我将整个 xml(正在处理的相同 xml)作为参数 传递$myxml

下面是我的示例输入 XML - 这只是 xml 的一个片段,我正在处理的实际 xml 文件太大并且包含如此复杂的树结构。但是这个示例 xml 足以产生我的问题。

输入文件
<?xml version="1.0" encoding="UTF-8"?>
<org xmlns="http://www.realestate.org/residential/2010/schemas">
    <depts>
        <dept>
            <deptId>1</deptId>
            <deptName>health</deptName>
            <deptAccessCode>HL007845</deptAccessCode>
        </dept>
    </depts>
    <employees>
        <employee>
            <name>TOM</name>
            <dept>
                <REFERENCE>
                    <LocationXPath>/org/depts/dept[1]</LocationXPath>
                </REFERENCE>
            </dept>
        </employee>
    </employees>
</org>

我的输出文件
<?xml version="1.0" encoding="UTF-8"?>
<org xmlns="http://www.realestate.org/residential/2010/schemas">
    <depts>
        <dept>
            <deptId>1</deptId>
            <deptName>health</deptName>
            <deptAccessCode>HL007845</deptAccessCode>
        </dept>
    </depts>
    <employees>
        <employee>
            <name>TOM</name>
            <dept xmlns="">
                <deptId>1</deptId>
                <deptName>health</deptName>
                <deptAccessCode>HL007845</deptAccessCode>
            </dept>
        </employee>
    </employees>
</org>  

预期输出在哪里
<?xml version="1.0" encoding="UTF-8"?>
<org xmlns="http://www.realestate.org/residential/2010/schemas">
    <depts>
        <dept>
            <deptId>1</deptId>
            <deptName>health</deptName>
            <deptAccessCode>HL007845</deptAccessCode>
        </dept>
    </depts>
    <employees>
        <employee>
            <name>TOM</name>
            <dept>
                <deptId>1</deptId>
                <deptName>health</deptName>
                <deptAccessCode>HL007845</deptAccessCode>
            </dept>
        </employee>
    </employees>
</org>

所以我在替换的根元素中的 << dept xmlns="">> 中得到了不需要的空 namespace 。
希望这可以清楚地解释我的问题
提前致谢

最终,我在下面的链接中找到了删除不需要的空 namespace 的解决方案。
http://social.msdn.microsoft.com/forums/en-US/xmlandnetfx/thread/0de59291-ef3a-4a4c-9ca5-17923b16a504

这是新的 XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tib="http://www.tibco.com/bw/xslt/custom-functions"
xmlns="http://www.realestate.org/residential/2010/schemas" >

<xsl:output omit-xml-declaration="no" indent="yes" method = "xml" />
<xsl:strip-space elements="*"/>
<xsl:param name="myxml"  />
<xsl:template match="node()|@*">
    <xsl:param name="isNodeToReplace"><xsl:call-template name="ReferenceCheck" /></xsl:param>
    <xsl:choose>
        <xsl:when test="$isNodeToReplace='true'">
            <xsl:call-template name="replaceWithData">
                <xsl:with-param name="ref"><xsl:value-of select="." /></xsl:with-param>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="ReferenceCheck">
    <xsl:choose>
        <xsl:when test="name(child::*[1])='REFERENCE' and name(child::*[1]//child::*[1])='LocationXPath'">true</xsl:when>
        <xsl:otherwise>false</xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="replaceWithData">
    <xsl:param name="ref" />
    <xsl:apply-templates select="tib:evaluate($myxml,$ref)" mode="move-to-namespace">
        <xsl:with-param name="namespace" select="'http://www.realestate.org/residential/2010/schemas'" />
    </xsl:apply-templates>
</xsl:template>
<xsl:template match="*" mode="move-to-namespace">
    <xsl:param name="namespace" />
    <xsl:element name="{local-name()}" namespace="{$namespace}">
        <xsl:copy-of select="@*" />
        <xsl:apply-templates select="node()" mode="move-to-namespace">
            <xsl:with-param name="namespace" select="$namespace"/>
        </xsl:apply-templates>
    </xsl:element>
</xsl:template>

<xsl:template match="text() | comment() | processing-instruction()" mode="move-to-namespace">
    <xsl:copy/>
</xsl:template>

</xsl:stylesheet>

如果有任何 xslt 专家进一步改进它以避免任何不必要的说明并提供适当的解释,那将非常高兴。

提前致谢

最佳答案

一般 :

  • 在纯 XSLT 1.0 中不可行。
  • 在纯 XSLT 2.0 中不可能
  • 在纯 XSLT 3.0 中可能是可能的——阅读关于
    <xsl:evaluate> 操作说明。
  • 在 XSLT 1.0 中,如果您的 XSLT 处理器实现了 EXSLT ,您可能会很幸运。 dyn:evaluate() 扩展功能(少数人做)。
    否则,您将不得不编写一个扩展函数来选择
    节点并将它们返回。

  • 如果对 XPath 表达式的语法有限制,那么有可能实现纯 XSLT 1.0 解决方案。

    关于xpath - xslt 1.0 用同一 xml 中的实际数据替换 xpath 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13558418/

    相关文章:

    javascript - jQuery 中的这个选择器语法发生了什么变化?

    php - 使用 PHP、simplexml_load_file 和 Xpath 从 XML 文件返回唯一值

    html - 使用经典 ASP 替换 HTML 中的文本

    javascript - 循环到单个语句

    javascript - 如何在 html 标签中查找字符串并使用 javascript/jquery 替换

    xml - 通过 Camel 的 xpath 中字符的索引获取子字符串

    python - Scrapy - python - 网络爬虫。输出所有 xpath 的列表,而不是仅第一个匹配项

    c - 未解析的外部符号、多个项目、Visual C++ 链接器错误?

    c++ - std::move 是否与左值引用一起使用? std::move 如何在标准容器上工作?

    c++ - 引用与指针特化