한 노드에서 다른 노드로 요소를 복사하고 싶습니다. 예를 들어, 내 입력 xml은 다음과 같습니다.
<?xml version="1.0" encoding="UTF-8"?>
<Institutions>
<Schools>
<schoolOne>schoolOne</schoolOne>
<scholTwo>scholTwo</scholTwo>
</Schools>
<Colleges>
<CollegeOne>CollegeOne</CollegeOne>
<CollegeTwo>CollegeTwo</CollegeTwo>
</Colleges>
</Institutions>
노드 <CollegeTwo>CollegeTwo</CollegeTwo>
아래 로 이동하고 싶습니다 <Schools>
. 즉, 내 출력 xml은 다음과 같습니다.
<?xml version="1.0" encoding="UTF-8"?>
<Institutions>
<Schools>
<schoolOne>schoolOne</schoolOne>
<scholTwo>scholTwo</scholTwo>
<CollegeTwo>CollegeTwo</CollegeTwo>
</Schools>
<Colleges>
<CollegeOne>CollegeOne</CollegeOne>
<CollegeTwo>CollegeTwo</CollegeTwo>
</Colleges>
</Institutions>
이를 달성하는 데 도움을 주시면 감사하겠습니다. 미리 감사드립니다.
다음 코드로 시도했지만 작동하지 않습니다.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:copy>
<xsl:apply-templates select="//Colleges/CollegeTwo" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
신원 변환을 포함하여 올바른 방향으로 시작했습니다.
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
그 자체로 노드를 그대로 복사하므로 변경하려는 노드에 대한 템플릿 만 작성하면됩니다 (XSLT는 특정 요소와 일치하는 템플릿에 우선 순위를 부여합니다).
귀하의 경우에는 Schools 요소에 새 하위 항목을 추가하고 싶으 므로이 요소와 일치하도록 두 번째 템플릿을 변경해야합니다 ( /
현재와 일치하도록 설정하면 문서 노드와 일치하지만이 경우 원하는 항목이 아닙니다). ).
<xsl:template match="Schools">
이미 CollegeTwo 요소 를 복사하는 코드가 있지만 이 시점에서 Schools 의 기존 자식 노드 도 복사 되는지 확인해야합니다 . ( xsl : copy 는 현재 노드를 복사 하지만 그 속성이나 자식은 복사 하지 않습니다).
이 XSLT 시도
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="Schools">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
<xsl:apply-templates select="//Colleges/CollegeTwo" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다